﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Module_Debug
{
    public partial class rm69298 : Form
    {
        //---------------------R66455 the dll function define-----------------------------------

        //De_Mura
        byte[] Erase_Flash_SyR66455_601_SSD =
        {
            0x0d,0x01,0x40,0xc0,0x01,0x00,0xb7,0x07,0x42,0xb1,
            0x01,0x01,0xb2,0x05,0x1b,0xb3,0x44,0x59,0xb4,0x04,
            0x38,0xb5,0x08,0x70,0xb6,0x00,0x37,0xde,0x00,0x03,
            0xb9,0x00,0x00,0xba,0xc2,0x50,0xbb,0x00,0x0c,0xd6,
            0x00,0x05,0xb9,0x00,0x01,0xb8,0x00,0x00,0xc9,0x1b,
            0x09,0xca,0x3a,0x0c,0xcb,0x03,0x1b,0xcc,0x0f,0x14,
            0xdf,0x00,0x3d,0xb7,0x07,0x4b,0xff,0x98,0x21
        };

        byte[] SyR66455_621_DeMura_ON_Part_0 =
        {
            0x30,0x01,0x02,0xb0,0x80,0x8a,0xb3
        };

        byte[] SyR66455_621_DeMura_ON_Part_1 =
        {
            0x30,0x01,0x3c,0xd4,0x93,0x90,0x40,0x1e,0xe1,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x0f,0xff,0x00,0x00,0x0f,0xff,0x00,
            0x00,0x0f,0xff,0x00,0x00,0x03,0x20,0x00,0xdf,0x00,
            0xc8,0x01,0x00,0x4d,0x8b,0x00,0x4d,0x8b,0x00,0x4d,
            0x8b,0x03,0x38,0xea,0x03
        };

        byte[] SyR66455_621_DeMura_OFF_Part_0 =
{
            0x30,0x01,0x02,0xb0,0x80,0x8a,0xb3
        };

        byte[] SyR66455_621_DeMura_OFF_Part_1 =
        {
            0x30,0x01,0x3c,0xd4,0x90,0x90,0x40,0x1e,0xe1,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x0f,0xff,0x00,0x00,0x0f,0xff,0x00,
            0x00,0x0f,0xff,0x00,0x00,0x03,0x20,0x00,0xdf,0x00,
            0xc8,0x01,0x00,0x4d,0x8b,0x00,0x4d,0x8b,0x00,0x4d,
            0x8b,0x03,0x38,0x15,0xc0
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_0 =
        {
            0x0d,0x02,0x48,0x02,0xb0,0x00,0x33,0xdf,0x00,0x00,
            0x58,0x83,0x2d,0x05,0x35,0x15,0x01,0x31,0x11,0x0b,
            0x50,0x00,0x02,0x00,0x00,0x43,0x84,0x00,0x10,0x00,
            0xdf,0xd4,0x0e,0x00,0x00,0x13,0xff,0x8c,0x27,0x17,
            0x0f,0x00,0x55,0x18,0x0f,0x00,0x1a,0x0f,0x00,0x1c,
            0x0f,0x00,0x1e,0x0f,0x00,0x00,0x00,0x00,0x06,0xf3,
            0x9f,0x00,0x00,0x00,0x02,0x02,0xf2,0x09,0x02,0x37,
            0x03,0x02,0x14,0xf4,0xff,0xd6,0xbe
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_1 =
        {
            0x30,0x02,0x04,0xf4,0x00,0x00,0x00,0xff,0xb9
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_2 =
        {
            0x0d,0x03,0x03,0xb7,0x07,0x4b,0x2e,0x0a
        };
        byte[] SyR66455_Read_DeMura_Flash_ID_3 =
        {
            0x0a,0x07,0x15,0xff,0xff,0xff,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0xd5,0x70
        };
        byte[] SyR66455_Read_DeMura_Flash_ID_4 =
        {
            0x0d,0x04,0x02,0x14,0xf4,0xf4,0xcb
        };

        byte[] Test1 =
        {
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] Test2 =
        {
            0x30,0x01,0x33,0xdf,0x00,0x00,0x58,0x83,0x2d,0x05,
            0x35,0x15,0x01,0x31,0x11,0x0b,0x50,0x00,0x02,0x00,
            0x00,0x43,0x84,0x00,0x10,0x00,0xdf,0xd4,0x0e,0x00,
            0x00,0x13,0xff,0x8c,0x27,0x17,0x0f,0x00,0x55,0x18,
            0x0f,0x00,0x1a,0x0f,0x00,0x1c,0x0f,0x00,0x1e,0x0f,
            0x00,0x00,0x00,0x00,0x82,0xec
        };
        byte[] Test3 =
        {
            0x30,0x01,0x06,0xf3,0x9f,0x00,0x00,0x00,0x02,0x36,
            0xeb
        };
        byte[] Test4 =
        {
            0x30,0x01,0x02,0xf2,0x09,0xe1,0xbc
        };
        byte[] Test5 =
        {
            0x30,0x01,0x02,0x37,0x03,0xa9,0x57
        };
        byte[] Test6 =
        {
            0x30,0x01,0x02,0x14,0xf4,0x65,0x1a
        };
        byte[] Test7 =
        {
            0x30,0x02,0x04,0xf4,0x00,0x00,0x00,0xff,0xb9
        };

        byte[] SyR66455_Read_DeMura_CheckSum_0 =
{
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] SyR66455_Read_DeMura_CheckSum_1 =
        {
            0x30,0x01,0x01,0x0b,0x1d,0x43
        };
        byte[] SyR66455_Read_DeMura_CheckSum_2 =
        {
            0x30,0x02,0x04,0xe7,0x00,0x00,0x00,0x7f,0xc2
        };




        byte[] Erase_Flash_SyR66455_601_MIPI =
        {
            0x0d,0x12,0x1c,0x02,0xb0,0x00,0x33,0xdf,0x00,0x40,
            0x58,0x83,0x2d,0x05,0x35,0x15,0x01,0x31,0x11,0x6b,
            0x50,0x00,0x02,0x00,0x00,0x43,0x84,0x00,0x10,0x00,
            0xdf,0xd4,0x0e,0x00,0x00,0x13,0xff,0x8c,0x27,0x17,
            0x0f,0x00,0x55,0x18,0x0f,0x00,0x1a,0x0f,0x00,0x1c,
            0x0f,0x00,0x1e,0x0f,0x00,0x00,0x00,0x00,0x02,0xb0,
            0x00,0x06,0xf3,0x06,0x00,0x00,0x00,0x00,0x04,0xff,
            0xff,0xff,0x0a,0x02,0xf2,0x11,0x02,0xb0,0x00,0x06,
            0xf3,0x05,0x00,0x00,0x00,0x00,0x04,0xff,0xff,0xff,
            0x0a,0x02,0xf2,0x09,0x02,0xb0,0x00,0x02,0x37,0x02,
            0x04,0xff,0xff,0xff,0x0a,0x02,0x14,0xf4,0x04,0xff,
            0xff,0xff,0x0a,0x02,0xb0,0x00,0x06,0xf3,0x60,0x00,
            0x00,0x00,0x00,0x04,0xff,0xff,0xff,0x0a,0x02,0xf2,
            0x11,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0xff,0x2b,0xf0,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };
        byte[] Erase_Flash_SyR66455_601_MIPI_1 =
        {
            0x0d,0x02,0x58,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x02,0xb0,0x00,0x06,0xf3,0x05,0x00,
            0x00,0x00,0x00,0x04,0xff,0xff,0xff,0x0a,0x02,0xf2,
            0x09,0x02,0xb0,0x04,0x02,0x37,0x02,0x04,0xff,0xff,
            0xff,0x0a,0x02,0x14,0xf4,0x04,0xff,0xff,0xff,0x0a,
            0xff,0x07,0x3e
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_Init =//初始化部分
        {
            0x30,0x0f,0x02,0xb0,0x00,0xB9,0x61
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_0 =//初始化部分
        {
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_1 =//初始化部分
        {
            0x30,0x01,0x33,0xdf,0x00,0x40,0x58,0x83,0x2d,0x05,
            0x35,0x15,0x01,0x31,0x11,0x6b,0x50,0x00,0x02,0x00,
            0x00,0x43,0x84,0x00,0x10,0x00,0xdf,0xd4,0x0e,0x00,
            0x00,0x13,0xff,0x8c,0x27,0x17,0x0f,0x00,0x55,0x18,
            0x0f,0x00,0x1a,0x0f,0x00,0x1c,0x0f,0x00,0x1e,0x0f,
            0x00,0x00,0x00,0x00,0xa0,0x0c
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_2 =//循环部分
        {
            0x30,0x01,0x02,0xb0,0x04,0x5b,0xbf
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_3 =//循环部分
        {
            0x30,0x01,0x06,0xf3,0x06,0x00,0x00,0x00,0x00,0x98,
            0x5f
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_4 =//循环部分
        {
            0x30,0x01,0x02,0xf2,0x11,0x72,0x85
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_5 =//循环部分_变化_数据地址
        {
            0x30,0x01,0x06,0xf3,0x02,0x00,0x0f,0x00,0x7f,0xb2,
            0x10
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_6 =//循环部分_数据结尾
{
            0x30,0x01,0x02,0xf2,0x1b,0xd3,0xcf
        };


        //De_Mura
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        //==============ca310===============
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        private bool ca310calFlag = false;
        private int[] tmp_gamma_195 = new int[93];
        private int[] tmp_gamma_160 = new int[33];
        private int[] tmp_gamma_162 = new int[75];
        private int[] tmp_gamma_120 = new int[78];
        private int[] tmp_gamma_010 = new int[75];
        private int[] tmp_gamma_350 = new int[75];
        private int[] tmp_gamma_300 = new int[75];

        //==============ca310===============

        private int CAS200_Measure_Gray = 255;
        private int CAS200_Measure_Count = 0;
        private int CA200_Measure_CE = 0;
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private byte[] Rec_tmp = new byte[1];//
        private byte[] Rec_tmp1 = new byte[1];//
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        public byte start_trans_flag = 0;
        public byte continueflag = 0;
        public byte RM19X_0xFE_XX = 0;

        public byte Seil_port_Find_flag = 0;
        private byte[] Driver_IC = new byte[1];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;

        CRC16 CRCData = new CRC16();

        //public frmStm32updata1(frmMain Frm)
        //{
        //    MainForm = Frm;
        //    InitializeComponent();
        //}

        frmMain MainForm = null;
        public rm69298(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
            //MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        }
        //private void System.Windows.Forms.(object sender, EventArgs e)
        //{
        //    MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        //}

        private void Frm_Activated(object sender, EventArgs e)
        {
            if (MainForm.ComEventRegFlag == 0)
            {
                MainForm.ComEventRegFlag = 1;
                //MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);

            }

            //this.Activated -= new System.EventHandler(this.Frm_Activated);

        }

        //是否已经绑定了事件        
        private bool IsBindEvent(Type type, Control con, string eventName)
        {
            bool isBind = false;

            PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);   //获取type类定义的所有事件的信息
            EventHandlerList ehl = (EventHandlerList)pi.GetValue(con, null);    //获取con对象的事件处理程序列表
            FieldInfo fieldInfo = (typeof(Control)).GetField("EventText", BindingFlags.Static | BindingFlags.NonPublic); //获取Control类Click事件的字段信息
            Delegate d = ehl[fieldInfo.GetValue(null)];
            if (d == null)
            {
                return isBind;
            }
            foreach (Delegate del in d.GetInvocationList())
            {
                if (del.Method.Name == eventName)
                {
                    isBind = true;
                    break;
                }
            }
            return isBind;
        }
        private void asdftest_dfa(object sender, EventArgs e)
        {
            // bool isBind = IsBindEvent(MainForm.comPort.GetType(), MainForm.comPort, "comboBox_TextChanged");
            //为Button1绑定一个事件处理程序
            Button btn = new Button();
            btn.Click += new
            EventHandler(Frm_Activated);
            //获取Button类定义的所有事件的信息
            PropertyInfo pi =
            (typeof(Button)).GetProperty("Events", BindingFlags.Instance |
            BindingFlags.NonPublic);



        }

        //{
        //    if (!isBind)

        //    {
        //         MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        //    }
        //}
        //=================================Timer====Start=========================
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();//获取焦点
            textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
            textBox1.ScrollToCaret();//滚动到光标处
        }
        //private void T2_Tick(object sender, EventArgs e)
        //{

        //    toolStripStatusLabel2.Text = DateTime.Now.ToString();

        //    if (Setting.TempRecord.VerChecking)
        //    {
        //        Send_buf = new byte[25];
        //        Setting.TempRecord.cmdLen = 25;
        //        Send_buf[0] = 0x0A; //LCD配置标志
        //        Send_buf[1] = 0x0D;
        //        Send_buf[2] = 0x15; //数据长度
        //        Send_buf[3] = FW_Version;
        //        Send_buf[4] = FW_SubVersion;
        //        CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);

        //        try
        //        {
        //            MainForm.comPort.Write(Send_buf, 0, 25);
        //            Setting.TempRecord.VerChecking = false;
        //        }
        //        catch
        //        {
        //            MessageBox.Show("串口有误，请检查", "错误");
        //            Setting.TempRecord.VerChecking = false;
        //        }
        //        //}
        //    }
        //}
        //=================================Timer====end=========================
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                MainForm.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)          //返回 STM32内部 flash 的配置文件数据
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL) //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//正确分析一条数据，从缓存中移除数据。
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)     //CRC校验出错
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);//复制一条完整数据到具体的数据缓存   
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);//正确分析一条数据，从缓存中移除数据。
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)     //STM32 更新bin文件成功  返回
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)       //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));//正确分析一条数据，从缓存中移除数据。
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (MainForm.comPort.IsOpen == true) MainForm.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }
                    else if (((buffer[0] == 0x1E) || (buffer[0] == 0x0E) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x09) || (buffer[0] == 0x08) || (buffer[0] == 0x19) || (buffer[0] == 0x29) || (buffer[0] == 0x2A) || (buffer[0] == 0x09) || (buffer[0] == 0x2E) || (buffer[0] == 0x2B) || (buffer[0] == 0x2C) || (buffer[0] == 0x2D) || (buffer[0] == 0x2F) || (buffer[0] == 0x30) || (buffer[0] == 0x31) || (buffer[0] == 0x3C) || (buffer[0] == 0x3D) || (buffer[0] == 0x4A) || (buffer[0] == 0x4C)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false; //停止超时计数

                        //------------------------------------------------------------------------------------------------------------------------
                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))  //RM67195 & RM67295 & RM67198&RM69298
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        MessageBox.Show("该功能已经删除！");
                                    }
                                    break;
                                case 0x03:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "写51h寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        MessageBox.Show("该功能已经删除！");
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //写OTP烧录次数
                                        {
                                            textBox1.Text = textBox1.Text + "OTP次数\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_50 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_50 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录  
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_40 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_40 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_60 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_60 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_70 烧录\tOK！\r\n";//zangqiang 20171120
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_70 烧录\tNG！\r\n";//zangqiang 20171120
                                    }
                                    break;
                                case 0x07:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_90 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_90 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;

                                //zangqiang 20171120
                                case 0x17:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_A0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_A0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                //zangqiang 20171120

                                //zangqiang 20171121
                                case 0x18:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_D0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_D0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                case 0x19:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_E0 烧录\tOK！\r\n";//zangqiang 20171121
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_E0 烧录\tNG！\r\n";//zangqiang 20171121
                                    }
                                    break;
                                //zangqiang 20171121

                                //zangqiang 20171125
                                case 0x1A:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_10 烧录\tOK！\r\n";//zangqiang 20171125
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_10 烧录\tNG！\r\n";//zangqiang 20171125
                                    }
                                    break;
                                case 0x1B:
                                    {
                                        if (Rec_tmp[4] == 0x00)  //OTP烧录
                                        {
                                            textBox1.Text = textBox1.Text + "OTP_Page_12 烧录\tOK！\r\n";//zangqiang 20171125
                                        }
                                        else
                                            textBox1.Text = textBox1.Text + "OTP_Page_12 烧录\tNG！\r\n";//zangqiang 20171125
                                    }
                                    break;
                                //zangqiang 20171125


                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if (Rec_tmp[3] == 0x00)
                                                textBox1.Text = textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if (Rec_tmp[3] == 0x42)
                                                textBox1.Text = textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if (Rec_tmp[3] == 0x81)
                                                textBox1.Text = textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x09:
                                    {
                                        MessageBox.Show("该功能已经删除！");
                                    }
                                    break;
                            }
                        }

                        ////---------------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x3C))   // 297/298/299  //350
                            {
                                if (Rec_tmp[7] == 0x2E)// 297/298/299
                                {
                                    textBox1.Text = textBox1.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x3C) //350
                                {
                                    textBox1.Text = textBox1.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:///350 only
                                        {
                                            if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x05:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x06:
                                        {
                                            textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x07:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                        }
                                        break;
                                    case 0x08:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                        }
                                        break;
                                    case 0x09:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        textBox1.Focus();//获取焦点
                        textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                        textBox1.ScrollToCaret();//滚动到光标处
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;

            }

        }

        private void RM67198_rbt_Gamma_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btnTmp = (RadioButton)sender;
            if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
            {
                RM67198_gamma_point_cs.Text = "RM67198绑点切换";
            }
            else if (MainForm.IC_comboBox.SelectedIndex == 10)
            {
                RM67198_gamma_point_cs.Text = "RM69298绑点切换";
            }
            switch (btnTmp.Name)
            {
                case "RM67198_rbt_33GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "01";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        NoRMR1.Enabled = true; NoRMG1.Enabled = true; NoRMB1.Enabled = true;
                        NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
                        NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
                        NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
                        NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
                        NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
                        NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
                        NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
                        NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
                        NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
                        NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
                        NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
                        NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
                        NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
                        NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

                    }
                    break;
                case "RM67198_rbt_31GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
                        NoRMR2.Enabled = false; NoRMG2.Enabled = false; NoRMB2.Enabled = false;
                        NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
                        NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
                        NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
                        NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
                        NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
                        NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
                        NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
                        NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
                        NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
                        NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
                        NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
                        NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
                        NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

                    }
                    break;
                case "RM67198_rbt_19GP":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "03";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();

                        NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
                        NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
                        NoRMR3.Enabled = false; NoRMG3.Enabled = false; NoRMB3.Enabled = false;
                        NoRMR28.Enabled = false; NoRMG28.Enabled = false; NoRMB28.Enabled = false;
                        NoRMR44.Enabled = false; NoRMG44.Enabled = false; NoRMB44.Enabled = false;
                        NoRMR84.Enabled = false; NoRMG84.Enabled = false; NoRMB84.Enabled = false;
                        NoRMR116.Enabled = false; NoRMG116.Enabled = false; NoRMB116.Enabled = false;
                        NoRMR148.Enabled = false; NoRMG148.Enabled = false; NoRMB148.Enabled = false;
                        NoRMR180.Enabled = false; NoRMG180.Enabled = false; NoRMB180.Enabled = false;
                        NoRMR188.Enabled = false; NoRMG188.Enabled = false; NoRMB188.Enabled = false;
                        NoRMR204.Enabled = false; NoRMG204.Enabled = false; NoRMB204.Enabled = false;
                        NoRMR212.Enabled = false; NoRMG212.Enabled = false; NoRMB212.Enabled = false;
                        NoRMR220.Enabled = false; NoRMG220.Enabled = false; NoRMB220.Enabled = false;
                        NoRMR236.Enabled = false; NoRMG236.Enabled = false; NoRMB236.Enabled = false;
                        NoRMR248.Enabled = false; NoRMG248.Enabled = false; NoRMB248.Enabled = false;
                    }
                    break;
                case "RM67198_rbt_NorM":
                    {
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "10";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "53"; txtRM67195Data.Text = "20";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                        txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
                        RM196_Others_Click(RM67195_RegW, null);
                        Application.DoEvents();
                    }
                    break;
                case "RM67198_rbt_HBM":
                    {
                        if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
                        {
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "51";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                        }
                        else if (MainForm.IC_comboBox.SelectedIndex == 10)  //if RM69298
                        {
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                            txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "52";
                            RM196_Others_Click(RM67195_RegW, null);
                            Application.DoEvents();
                        }
                    }
                    break;
                default: break;
            }
        }

        //private void LCD_Cfg_Click(object sender, EventArgs e)
        //{
        //    MainForm.LCD_Cfg_Click( sender,  e);
        //}
        //=================================LCD cfg function:OLED_Show_Click====Start=================
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            UInt32 ICS307Temp = 0x00;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {
                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;
                //case "btnPatternR": Send_buf[1] = 0x08; break; //读取Pattern参数
                //case "btnReload": Send_buf[1] = 0x0A; break; //重新载入图片
                //case "btnPrevPic": Send_buf[1] = 0x0B; break; //上一张图片
                //case "btnNextPic": Send_buf[1] = 0x0C; break; //下一张图片
                //case "mtReadversion": Send_buf[1] = 0x0D; break; //读取程式版本
                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }

        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }
        ////-------------RM67198_rbt_Gamma_Points
        private void RM67198_rbt_Gamma_Points()
        {
            if (RM67198_rbt_33GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_33GP, null);
            }
            if (RM67198_rbt_31GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_31GP, null);
            }
            if (RM67198_rbt_19GP.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_19GP, null);
            }
            if (RM67198_rbt_HBM.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_HBM, null);
            }
            if (RM67198_rbt_NorM.Checked == true)
            {
                RM67198_rbt_Gamma_CheckedChanged(RM67198_rbt_NorM, null);
            }
        }

        //------------------------------------------Demura------------------------------------------------
        private void Demura_Click(object sender, EventArgs e)
        {

            if (MainForm.IC_comboBox.SelectedIndex == 13)  //是新思IC
            {
                Button btnTmp = (Button)sender;
                switch (btnTmp.Name)
                {
                    case "Demura_Function_ON":
                        {
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_ON_Part_0, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 50;
                            T1.Enabled = true;
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_ON_Part_1, 0, 65);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 100;
                            T1.Enabled = true;
                        }
                        break;
                    case "Demura_Function_OFF":
                        {
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_OFF_Part_0, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 50;
                            T1.Enabled = true;
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_OFF_Part_1, 0, 65);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 100;
                            T1.Enabled = true;
                        }
                        break;
                    case "Demura_Flash_Enable":
                        {
                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test1, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }


                            try
                            {
                                Setting.TempRecord.cmdLen = 56;
                                MainForm.comPort.Write(Test2, 0, 56);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 11;
                                MainForm.comPort.Write(Test3, 0, 11);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test4, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test5, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test6, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                            Thread.Sleep(50);


                            try
                            {
                                Setting.TempRecord.cmdLen = 9;
                                MainForm.comPort.Write(Test7, 0, 9);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }




                        }
                        break;

                    case "Flash_Check_Demura_data":
                        {
                            //try
                            //{
                            //    Setting.TempRecord.cmdLen = 7;
                            //    MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_0, 0, 7);
                            //}
                            //catch
                            //{
                            //    MessageBox.Show("串口有误，请检查", "错误");
                            //}
                            //try
                            //{
                            //    Setting.TempRecord.cmdLen = 6;
                            //    MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_1, 0, 6);
                            //}
                            //catch
                            //{
                            //    MessageBox.Show("串口有误，请检查", "错误");
                            //}
                            try
                            {
                                Setting.TempRecord.cmdLen = 9;
                                MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_2, 0, 9);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                        }
                        break;
                    case "Flash_Write_Demura_data":
                        {

                        }
                        break;



                    default: break;





                }
            }
            else//不是新思IC
            {
                //string[] RM67298_Enable_Flash_Control = new string[4] { "fe", "20", "f3", "02"};
                Button btnTmp = (Button)sender;
                Send_buf = new byte[10];
                Setting.TempRecord.cmdLen = 10;
                Send_buf[0] = 0x2F;
                Send_buf[2] = 0x05;
                if (MainForm.IC_comboBox.SelectedIndex == 10)  //if RM297/298/299
                {
                    Send_buf[7] = 0x2E;
                }
                else if (MainForm.IC_comboBox.SelectedIndex == 11)   //if RM350
                {
                    Send_buf[7] = 0x3C;
                }
                switch (btnTmp.Name)
                {
                    case "Demura_Function_ON":
                        {
                            Send_buf[1] = 0x07;
                        }
                        break;
                    case "Demura_Function_OFF":
                        {
                            Send_buf[1] = 0x08;
                        }
                        break;
                    case "Inter_Demura_data":
                        {
                            Send_buf[1] = 0x01;
                        }
                        break;
                    case "Exit_Demura_data":
                        {
                            Send_buf[1] = 0x02;
                        }
                        break;
                    case "Flash_Write_Demura_data":
                        {
                            Send_buf[1] = 0x03;
                        }
                        break;
                    case "Flash_Check_Demura_data":
                        {
                            Send_buf[1] = 0x04;
                        }
                        break;
                    case "Flash_Erase_Demura_data":
                        {
                            Send_buf[1] = 0x05;
                        }
                        break;
                    case "Demura_Flash_Enable":
                        {
                            Send_buf[1] = 0x06;
                        }
                        break;
                    case "Flash_Demura_OTP":
                        {
                            Send_buf[1] = 0x09;
                        }
                        break;
                    case "Flash_Write_Demura_data2":
                        {
                            Send_buf[1] = 0x0A;
                        }
                        break;
                    default:
                        break;
                }
                CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);


                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 10);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }
            }




        }

        private void btnReload_Demura_File_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fldb = new FolderBrowserDialog();
            fldb.ShowNewFolderButton = false;
            DialogResult res = fldb.ShowDialog();
            if (res != System.Windows.Forms.DialogResult.OK)
            {
                MessageBox.Show("没选择任何文件.");
                return;
            }
            string path = fldb.SelectedPath;

            if (!Directory.Exists(path))
            {
                MessageBox.Show("文件夹不存在.");
                return;
            }
            listBox3_Demura_File.Items.Clear();
            string s = null;
            DirectoryInfo d = new DirectoryInfo(path);
            FileInfo[] Files = d.GetFiles("*.hex");

            // List<string> lstr = new List<string>();

            foreach (FileInfo file in Files)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }

            //zangqiang 20180522  增加bin文件 识别
            FileInfo[] Files1 = d.GetFiles("*.bin");
            foreach (FileInfo file in Files1)
            {
                s = file.Name;
                listBox3_Demura_File.Items.Add(s);
            }
            //zangqiang 20180522  增加bin文件 识别

            textBox5.Text = path;
        }

        private void Load_Demura_data_Click(object sender, EventArgs e)
        {
            Int32 m_txlen = 0;
            Int32 Once_txlen = 0;
            Int32 File_Length, File_Length_tep;
            bool tx_en = true;
            byte[] TemFileContent = new byte[1024 * 100];
            char[] Acsii_array = new char[2];
            Int32 div = 0;
            div = Convert.ToInt16(MainForm.HACT.Value) * 3;
            TemFileContent[0] = 0x5B;
            //div = 1080*3;
            //div = 255;
            System.Int32 ByteReaded = 0x00;
            try
            {
                string FileName = textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
                FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                File_Length = Convert.ToInt32(aFile.Length);
                File_Length_tep = File_Length;
                while (tx_en)
                {
                    if (File_Length_tep > (File_Length % div))
                    {
                        Once_txlen = div;
                    }
                    else  //最后一笔
                    {
                        Once_txlen = Convert.ToInt32(aFile.Length % div);
                        tx_en = false;
                    }
                    aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
                    ByteReaded = aFile.Read(TemFileContent, 5, Once_txlen); // 存入第五位
                    try
                    {
                        TemFileContent[0] = 0x5B;
                        TemFileContent[1] = Convert.ToByte(File_Length >> 24);
                        TemFileContent[2] = Convert.ToByte((File_Length >> 16) & 0xff);
                        TemFileContent[3] = Convert.ToByte((File_Length >> 8) & 0xff);
                        TemFileContent[4] = Convert.ToByte(File_Length & 0xff);
                        MainForm.comPort.Write(TemFileContent, 0, Once_txlen + 5);
                    }
                    catch
                    {
                        MessageBox.Show("串口有误，请检查", "错误");
                        tx_en = false;
                        break;
                    }
                    //tx_en = false;
                    m_txlen += ByteReaded;
                    progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
                    label_flash.Text = progressBar2.Value.ToString() + "%";
                    Application.DoEvents();//重点，必须加上，否则父子窗体都假死
                    File_Length_tep = File_Length_tep - div;
                }
                Demura_Click(Flash_Write_Demura_data, null);

                {
                    MessageBox.Show("载入完成！");
                }
                aFile.Close();
            }
            catch
            {
                MessageBox.Show("请选择文件夹", "错误");
            }

        }
        private int[] Parttern_RM67195 = new int[30] { 255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;//Row_Accum 行累加器；

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);//为该表格增加一行
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        int Value10bits51;
        private void T3_Tick(object sender, EventArgs e)
        {
            if (check_follow_CE_Auto_measure.Checked == true)
            {
                string strLine = Convert.ToString(text_CE_data.Text);
                string[] arr = strLine.Split(','); //以逗号分割，添加到数组              
                if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
                {
                    txtRM67195Add.Text = arr[0];
                    txtRM67195Data.Text = CA200_Measure_CE.ToString("x");
                    RM196_Others_Click(RM67195_RegW, e);
                    Thread.Sleep(20);
                }

                CA200_Measure_CE = CA200_Measure_CE + int.Parse(CAS200_txt0.Text);

                if (CA200_Measure_CE > (byte.Parse((Convert.ToInt32(arr[arr.Length - 1], 16).ToString())) + Convert.ToInt32(text_CE_times.Text) - 1))
                    T3.Enabled = false;
            }

            else if (cbx_follow_51_points.Checked == true)
            {
                //RM298Reg51Write();
                //if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
                if (chk10bitsSelect.Checked == false)
                {
                    {
                        txtRM67195Add.Text = "FE";
                        txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, e);

                        txtRM67195Add.Text = "51";
                        txtRM67195Data.Text = CAS200_Measure_Gray.ToString("x");
                        RM196_Others_Click(RM67195_RegW, e);
                    }

                    CAS200_Measure_Gray = CAS200_Measure_Gray - Convert.ToInt32(CAS200_txt0.Text);
                    if (CAS200_Measure_Gray < 0)
                        T3.Enabled = false;
                }
                else
                {
                    {
                        txtRM67195Add.Text = "FE";
                        txtRM67195Data.Text = "00";
                        RM196_Others_Click(RM67195_RegW, e);

                        RM298Reg51Write(Value10bits51);


                    }

                    Value10bits51 = Value10bits51 - Convert.ToInt32(CAS200_txt0.Text);
                    if (Value10bits51 < 0)
                        T3.Enabled = false;
                }

            }
            else if (cbx_follow_gamma_points.Checked == true)  //follow gamma绑点
            {
                CAS200_Measure_Count++; //跳阶
                if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
                {
                    if (CAS200_Measure_Count < Parttern_RM67195.Length)
                    {
                        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
                        {
                            if (numPatternR.Value != 0)
                            {
                                numPatternR.Value = Parttern_RM67195[CAS200_Measure_Count];
                            }
                            if (numPatternG.Value != 0)
                            {
                                numPatternG.Value = Parttern_RM67195[CAS200_Measure_Count];
                            }
                            if (numPatternB.Value != 0)
                            {
                                numPatternB.Value = Parttern_RM67195[CAS200_Measure_Count];
                            }
                        }
                        else
                        {
                            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67195[CAS200_Measure_Count];
                        }
                        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                    }
                    else
                    {
                        CAS200_Measure_Count = 0;
                        T3.Enabled = false;
                    }
                }

            }
            else
            {
                if ((Convert.ToInt16(numPatternR.Value.ToString()) + Convert.ToInt16(numPatternB.Value.ToString()) + Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_txt0.Text)) >= 0)
                {
                    if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
                    {
                        if (numPatternR.Value != 0)
                        {
                            numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_txt0.Text));
                        }
                        if (numPatternG.Value != 0)
                        {
                            numPatternG.Value = (byte)(Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_txt0.Text));
                        }
                        if (numPatternB.Value != 0)
                        {
                            numPatternB.Value = (byte)(Convert.ToInt16(numPatternB.Value.ToString()) - int.Parse(CAS200_txt0.Text));
                        }
                    }
                    else
                    {
                        numPatternB.Value = numPatternG.Value = numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_txt0.Text));
                    }
                    Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                }
                else
                {
                    T3.Enabled = false;
                }
            }
            Thread.Sleep(50);
            btn_CAS200_Synt_Click(btn_CAS200_Measure, null); //量测
        }

        private void btn_CAS200_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;

            switch (btnTmp.Name)
            {
                case "btn_CAS200_Measure":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200Save":
                    {
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine(); //循环读取下一行
                                Line_Count++;
                            }
                            fs.Close(); //关闭流
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;//序号
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;

                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        string strLine = Convert.ToString(text_CE_data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                                                           //  Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                        CAS200_Measure_Count = 0;
                        CAS200_Measure_Gray = 255;
                        CA200_Measure_CE = byte.Parse((Convert.ToInt32(arr[arr.Length - 1], 16).ToString()));
                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);//延时
                        Value10bits51 = 0x3ff;
                    }
                    break;
                default: break;
            }
        }
        private void mtLoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();

                string[] arrtmp = strtmp.Split(new Char[] { ',' });




                if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198 || MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298 || MainForm.IC_comboBox.SelectedIndex == 33) //rm67198
                {
                    NoRMR0.Text = arrtmp[1].ToString(); NoRMG0.Text = arrtmp[3].ToString(); NoRMB0.Text = arrtmp[5].ToString();
                    NoRMR1.Text = arrtmp[7].ToString(); NoRMG1.Text = arrtmp[9].ToString(); NoRMB1.Text = arrtmp[11].ToString();
                    NoRMR2.Text = arrtmp[13].ToString(); NoRMG2.Text = arrtmp[15].ToString(); NoRMB2.Text = arrtmp[17].ToString();
                    NoRMR3.Text = arrtmp[19].ToString(); NoRMG3.Text = arrtmp[21].ToString(); NoRMB3.Text = arrtmp[23].ToString();
                    NoRMR4.Text = arrtmp[25].ToString(); NoRMG4.Text = arrtmp[27].ToString(); NoRMB4.Text = arrtmp[29].ToString();
                    NoRMR8.Text = arrtmp[31].ToString(); NoRMG8.Text = arrtmp[33].ToString(); NoRMB8.Text = arrtmp[35].ToString();
                    NoRMR12.Text = arrtmp[37].ToString(); NoRMG12.Text = arrtmp[39].ToString(); NoRMB12.Text = arrtmp[41].ToString();
                    NoRMR16.Text = arrtmp[43].ToString(); NoRMG16.Text = arrtmp[45].ToString(); NoRMB16.Text = arrtmp[47].ToString();
                    NoRMR20.Text = arrtmp[49].ToString(); NoRMG20.Text = arrtmp[51].ToString(); NoRMB20.Text = arrtmp[53].ToString();
                    NoRMR28.Text = arrtmp[55].ToString(); NoRMG28.Text = arrtmp[57].ToString(); NoRMB28.Text = arrtmp[59].ToString();
                    NoRMR36.Text = arrtmp[61].ToString(); NoRMG36.Text = arrtmp[63].ToString(); NoRMB36.Text = arrtmp[65].ToString();
                    NoRMR44.Text = arrtmp[67].ToString(); NoRMG44.Text = arrtmp[69].ToString(); NoRMB44.Text = arrtmp[71].ToString();
                    NoRMR52.Text = arrtmp[73].ToString(); NoRMG52.Text = arrtmp[75].ToString(); NoRMB52.Text = arrtmp[77].ToString();
                    NoRMR68.Text = arrtmp[79].ToString(); NoRMG68.Text = arrtmp[81].ToString(); NoRMB68.Text = arrtmp[83].ToString();
                    NoRMR84.Text = arrtmp[85].ToString(); NoRMG84.Text = arrtmp[87].ToString(); NoRMB84.Text = arrtmp[89].ToString();
                    NoRMR100.Text = arrtmp[91].ToString(); NoRMG100.Text = arrtmp[93].ToString(); NoRMB100.Text = arrtmp[95].ToString();
                    NoRMR116.Text = arrtmp[97].ToString(); NoRMG116.Text = arrtmp[99].ToString(); NoRMB116.Text = arrtmp[101].ToString();
                    NoRMR132.Text = arrtmp[103].ToString(); NoRMG132.Text = arrtmp[105].ToString(); NoRMB132.Text = arrtmp[107].ToString();
                    NoRMR148.Text = arrtmp[109].ToString(); NoRMG148.Text = arrtmp[111].ToString(); NoRMB148.Text = arrtmp[113].ToString();
                    NoRMR164.Text = arrtmp[115].ToString(); NoRMG164.Text = arrtmp[117].ToString(); NoRMB164.Text = arrtmp[119].ToString();
                    NoRMR180.Text = arrtmp[121].ToString(); NoRMG180.Text = arrtmp[123].ToString(); NoRMB180.Text = arrtmp[125].ToString();
                    NoRMR188.Text = arrtmp[127].ToString(); NoRMG188.Text = arrtmp[129].ToString(); NoRMB188.Text = arrtmp[131].ToString();
                    NoRMR196.Text = arrtmp[133].ToString(); NoRMG196.Text = arrtmp[135].ToString(); NoRMB196.Text = arrtmp[137].ToString();
                    NoRMR204.Text = arrtmp[139].ToString(); NoRMG204.Text = arrtmp[141].ToString(); NoRMB204.Text = arrtmp[143].ToString();
                    NoRMR212.Text = arrtmp[145].ToString(); NoRMG212.Text = arrtmp[147].ToString(); NoRMB212.Text = arrtmp[149].ToString();
                    NoRMR220.Text = arrtmp[151].ToString(); NoRMG220.Text = arrtmp[153].ToString(); NoRMB220.Text = arrtmp[155].ToString();
                    NoRMR228.Text = arrtmp[157].ToString(); NoRMG228.Text = arrtmp[159].ToString(); NoRMB228.Text = arrtmp[161].ToString();
                    NoRMR236.Text = arrtmp[163].ToString(); NoRMG236.Text = arrtmp[165].ToString(); NoRMB236.Text = arrtmp[167].ToString();
                    NoRMR244.Text = arrtmp[169].ToString(); NoRMG244.Text = arrtmp[171].ToString(); NoRMB244.Text = arrtmp[173].ToString();
                    NoRMR248.Text = arrtmp[175].ToString(); NoRMG248.Text = arrtmp[177].ToString(); NoRMB248.Text = arrtmp[179].ToString();
                    NoRMR252.Text = arrtmp[181].ToString(); NoRMG252.Text = arrtmp[183].ToString(); NoRMB252.Text = arrtmp[185].ToString();
                    NoRMR254.Text = arrtmp[187].ToString(); NoRMG254.Text = arrtmp[189].ToString(); NoRMB254.Text = arrtmp[191].ToString();
                    NoRMR255.Text = arrtmp[193].ToString(); NoRMG255.Text = arrtmp[195].ToString(); NoRMB255.Text = arrtmp[197].ToString();
                }

            }
        }

        private void mtSaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);

                    if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198 || MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298 || MainForm.IC_comboBox.SelectedIndex == 33) //rm67198
                    {
                        str = str + "R0," + NoRMR0.Text + "," + "G0," + NoRMG0.Text + "," + "B0," + NoRMB0.Text + "\r\n";//R
                        str = str + "R1," + NoRMR1.Text + "," + "G1," + NoRMG1.Text + "," + "B1," + NoRMB1.Text + "\r\n";
                        str = str + "R2," + NoRMR2.Text + "," + "G2," + NoRMG2.Text + "," + "B2," + NoRMB2.Text + "\r\n";
                        str = str + "R3," + NoRMR3.Text + "," + "G3," + NoRMG3.Text + "," + "B3," + NoRMB3.Text + "\r\n";
                        str = str + "R4," + NoRMR4.Text + "," + "G4," + NoRMG4.Text + "," + "B4," + NoRMB4.Text + "\r\n";
                        str = str + "R8," + NoRMR8.Text + "," + "G8," + NoRMG8.Text + "," + "B8," + NoRMB8.Text + "\r\n";
                        str = str + "R12," + NoRMR12.Text + "," + "G12," + NoRMG12.Text + "," + "B12," + NoRMB12.Text + "\r\n";
                        str = str + "R16," + NoRMR16.Text + "," + "G16," + NoRMG16.Text + "," + "B16," + NoRMB16.Text + "\r\n";
                        str = str + "R20," + NoRMR20.Text + "," + "G20," + NoRMG20.Text + "," + "B20," + NoRMB20.Text + "\r\n";
                        str = str + "R28," + NoRMR28.Text + "," + "G28," + NoRMG28.Text + "," + "B28," + NoRMB28.Text + "\r\n";
                        str = str + "R36," + NoRMR36.Text + "," + "G36," + NoRMG36.Text + "," + "B36," + NoRMB36.Text + "\r\n";
                        str = str + "R44," + NoRMR44.Text + "," + "G44," + NoRMG44.Text + "," + "B44," + NoRMB44.Text + "\r\n";
                        str = str + "R52," + NoRMR52.Text + "," + "G52," + NoRMG52.Text + "," + "B52," + NoRMB52.Text + "\r\n";
                        str = str + "R68," + NoRMR68.Text + "," + "G68," + NoRMG68.Text + "," + "B68," + NoRMB68.Text + "\r\n";//R
                        str = str + "R84," + NoRMR84.Text + "," + "G84," + NoRMG84.Text + "," + "B84," + NoRMB84.Text + "\r\n";
                        str = str + "R100," + NoRMR100.Text + "," + "G100," + NoRMG100.Text + "," + "B100," + NoRMB100.Text + "\r\n";
                        str = str + "R116," + NoRMR116.Text + "," + "G116," + NoRMG116.Text + "," + "B116," + NoRMB116.Text + "\r\n";
                        str = str + "R132," + NoRMR132.Text + "," + "G132," + NoRMG132.Text + "," + "B132," + NoRMB132.Text + "\r\n";
                        str = str + "R148," + NoRMR148.Text + "," + "G148," + NoRMG148.Text + "," + "B148," + NoRMB148.Text + "\r\n";
                        str = str + "R164," + NoRMR164.Text + "," + "G164," + NoRMG164.Text + "," + "B164," + NoRMB164.Text + "\r\n";
                        str = str + "R180," + NoRMR180.Text + "," + "G180," + NoRMG180.Text + "," + "B180," + NoRMB180.Text + "\r\n";
                        str = str + "R188," + NoRMR188.Text + "," + "G188," + NoRMG188.Text + "," + "B188," + NoRMB188.Text + "\r\n";
                        str = str + "R196," + NoRMR196.Text + "," + "G196," + NoRMG196.Text + "," + "B196," + NoRMB196.Text + "\r\n";
                        str = str + "R204," + NoRMR204.Text + "," + "G204," + NoRMG204.Text + "," + "B204," + NoRMB204.Text + "\r\n";
                        str = str + "R212," + NoRMR212.Text + "," + "G212," + NoRMG212.Text + "," + "B212," + NoRMB212.Text + "\r\n";//R
                        str = str + "R220," + NoRMR220.Text + "," + "G220," + NoRMG220.Text + "," + "B220," + NoRMB220.Text + "\r\n";
                        str = str + "R228," + NoRMR228.Text + "," + "G228," + NoRMG228.Text + "," + "B228," + NoRMB228.Text + "\r\n";
                        str = str + "R236," + NoRMR236.Text + "," + "G236," + NoRMG236.Text + "," + "B236," + NoRMB236.Text + "\r\n";
                        str = str + "R244," + NoRMR244.Text + "," + "G244," + NoRMG244.Text + "," + "B244," + NoRMB244.Text + "\r\n";
                        str = str + "R248," + NoRMR248.Text + "," + "G248," + NoRMG248.Text + "," + "B248," + NoRMB248.Text + "\r\n";
                        str = str + "R252," + NoRMR252.Text + "," + "G252," + NoRMG252.Text + "," + "B252," + NoRMB252.Text + "\r\n";
                        str = str + "R254," + NoRMR254.Text + "," + "G254," + NoRMG254.Text + "," + "B254," + NoRMB254.Text + "\r\n";
                        str = str + "R255," + NoRMR255.Text + "," + "G255," + NoRMG255.Text + "," + "B255," + NoRMB255.Text + "\r\n";
                        sw.WriteLine(str);
                    }

                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        CaSDK CaSDKObj = new CaSDK();
        private void btnCal_Click(object sender, EventArgs e)
        {
            try
            {
                //Tim1Ca310.Start();//显示提示310连接提示信息
                textBox1.Text += "CA310/410 连接校准中...\r\n";
                OLED_Show_Click(btnblack, e);
                Application.DoEvents();//重点，必须加上，否则提示信息不能按顺序显示出来
            }
            catch
            {
                Thread.Sleep(100);
            }
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.AutoConnect();
                    CaSDKObj.SingleCalibration(0, byte.Parse(CA310_Channel.Text), double.Parse(CA410_Measure_Freq.Text));
                }
                else
                {
                    CaSDKObj.CA310_Connect();
                    CaSDKObj.CA310_Initial(byte.Parse(CA310_Channel.Text));
                }
                //btnCal_manual.Text = "Wait"; Application.DoEvents();
                btn_CAS200_Start.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200Save.Enabled = true;
                btn_CAS200_Measure.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
                btn_CAS200_Stop.Enabled = true;
                //btnCal_manual.Text = "0Cal";
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310/410 Connect Fail!!" + EX.Message);
            }
            //btnCal_manual.BackColor = Color.Gray;
            //btnAutoGamma.Enabled = true;
            textBox1.Text += "CA310 连接完成...\r\n";
            //btnSetRGBBase.Enabled = true;
        }

        private void Msr_CA310()
        {
            try
            {
                if (rbt_ca410.Checked == true)
                {
                    CaSDKObj.CA410_Measure_Lxy();
                    auto_lum = CaSDKObj.Lv;
                    auto_x = CaSDKObj.sx;
                    auto_y = CaSDKObj.sy;
                }
                else //(rbt_ca310.Checked == true)
                {
                    MainForm.Msr_CASDK();
                    auto_lum = MainForm.CASDK_LUM;
                    auto_x = MainForm.CASDK_x;
                    auto_y = MainForm.CASDK_y;
                }
                RM692CX_X.Text = auto_x + "";
                RM692CX_Y.Text = auto_y + "";
                RM692CX_Lum.Text = auto_lum + "";
            }
            catch
            {
                textBox1.Text += ("CA310/410未校准或者镜头被遮挡或读数异常！\r\n");
                //textBox1.Focus();//获取焦点
                textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
                return;
            }
        }

        //=================================LCD cfg function: OLED_Show_Click====end=================

        private void RM67x95_trackBar_Scroll(object sender, EventArgs e)
        {
            RM67X95_label.Text = RM67x95_trackBar.Value.ToString() + "阶";
            if (RM19X_0xFE_XX != 0xF0)
            {
                RM19X_0xFE_XX = 0xF0;
                txtRM67195Add.Text = "fe";
                txtRM67195Data.Text = "00";
                RM196_Others_Click(RM67195_W_Reg51, e);
            }
            txtRM67195Add.Text = "51";
            txtRM67195Data.Text = RM67x95_trackBar.Value.ToString("x");
            RM196_Others_Click(RM67195_W_Reg51, e);
        }

        private void RM67195_GAMMA_Click(object sender, EventArgs e)//67195的Gamma 读写
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[68];
            Setting.TempRecord.cmdLen = 68;
            Send_buf[2] = 63;
            Send_buf[3] = 0;

            if (MainForm.IC_comboBox.SelectedIndex == 4)  //if RM67295
            {
                Send_buf[0] = 0x19;
            }
            else if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
            {
                if (RM67198_rbt_33GP.Checked == true)
                {
                    Send_buf = new byte[72];
                    Setting.TempRecord.cmdLen = 72;
                    Send_buf[2] = 67;
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    Send_buf = new byte[44];
                    Setting.TempRecord.cmdLen = 44;
                    Send_buf[2] = 39;
                }
                //------------------------
                if (RM67198_rbt_NorM.Checked == true) //198 正常亮度gamma
                {
                    Send_buf[3] = 0;
                }
                else if (RM67198_rbt_HBM.Checked == true) //198 高亮gamma
                {
                    Send_buf[3] = 0x08;
                }
                Send_buf[0] = 0x29;
            }
            else if ((MainForm.IC_comboBox.SelectedIndex == 10) || (MainForm.IC_comboBox.SelectedIndex == 33)) //if RM69298
            {
                if (RM67198_rbt_33GP.Checked == true)
                {
                    Send_buf = new byte[72];
                    Setting.TempRecord.cmdLen = 72;
                    Send_buf[2] = 67;
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    Send_buf = new byte[44];
                    Setting.TempRecord.cmdLen = 44;
                    Send_buf[2] = 39;
                }
                //------------------------
                if (RM67198_rbt_NorM.Checked == true) //198 正常亮度gamma
                {
                    Send_buf[3] = 0;
                }
                else if (RM67198_rbt_HBM.Checked == true) //198 高亮gamma
                {
                    Send_buf[3] = 0x08;
                }
                Send_buf[0] = 0x2e;
            }
            else
            {
                Send_buf[0] = 0x09; //RM67195
            }


            switch (btnTmp.Name)
            {
                //--------------------------------------
                case "RM67195_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x00;
                        if (RM67198_rbt_33GP.Checked == true)//RM67198 33绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMR0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMR0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMR1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMR1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMR2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMR2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMR3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMR3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMR4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMR4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMR8.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMR8.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMR12.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMR12.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMR16.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMR16.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMR20.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMR20.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMR28.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMR28.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMR36.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMR36.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMR44.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMR44.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMR52.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMR52.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMR68.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMR68.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMR84.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMR84.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMR100.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMR100.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMR116.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMR116.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMR132.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMR132.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMR148.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMR148.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMR164.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMR164.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMR180.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMR180.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMR188.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMR188.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMR196.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMR196.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMR204.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMR204.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMR212.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMR212.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMR220.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMR220.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMR228.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMR228.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMR236.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMR236.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMR244.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMR244.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMR248.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMR248.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMR252.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMR252.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(NoRMR254.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(NoRMR254.Value.ToString())) & 0xff);
                            Send_buf[68] = Convert.ToByte(Convert.ToUInt16(NoRMR255.Value.ToString()) >> 8);
                            Send_buf[69] = Convert.ToByte((Convert.ToUInt16(NoRMR255.Value.ToString())) & 0xff);
                        }
                        else if (RM67198_rbt_19GP.Checked == true)//RM67198 19绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMR0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMR0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMR2.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMR2.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMR4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMR4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMR8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMR8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMR12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMR12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMR16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMR16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMR20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMR20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMR36.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMR36.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMR52.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMR52.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMR68.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMR68.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMR100.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMR100.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMR132.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMR132.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMR164.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMR164.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMR196.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMR196.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMR228.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMR228.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMR244.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMR244.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMR252.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMR252.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMR254.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMR254.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMR255.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMR255.Value.ToString())) & 0xff);
                        }
                        else //RM67195&RM67295&RM67198 31绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMR0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMR0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMR3.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMR3.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMR4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMR4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMR8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMR8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMR12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMR12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMR16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMR16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMR20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMR20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMR28.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMR28.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMR36.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMR36.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMR44.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMR44.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMR52.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMR52.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMR68.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMR68.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMR84.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMR84.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMR100.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMR100.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMR116.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMR116.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMR132.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMR132.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMR148.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMR148.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMR164.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMR164.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMR180.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMR180.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMR188.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMR188.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMR196.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMR196.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMR204.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMR204.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMR212.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMR212.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMR220.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMR220.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMR228.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMR228.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMR236.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMR236.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMR244.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMR244.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMR248.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMR248.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMR252.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMR252.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMR254.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMR254.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMR255.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMR255.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "RM67195_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x42;
                        if (RM67198_rbt_33GP.Checked == true)//RM67198 33绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMG0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMG0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMG1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMG1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMG2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMG2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMG3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMG3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMG4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMG4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMG8.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMG8.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMG12.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMG12.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMG16.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMG16.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMG20.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMG20.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMG28.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMG28.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMG36.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMG36.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMG44.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMG44.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMG52.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMG52.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMG68.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMG68.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMG84.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMG84.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMG100.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMG100.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMG116.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMG116.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMG132.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMG132.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMG148.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMG148.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMG164.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMG164.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMG180.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMG180.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMG188.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMG188.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMG196.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMG196.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMG204.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMG204.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMG212.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMG212.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMG220.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMG220.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMG228.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMG228.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMG236.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMG236.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMG244.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMG244.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMG248.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMG248.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMG252.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMG252.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(NoRMG254.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(NoRMG254.Value.ToString())) & 0xff);
                            Send_buf[68] = Convert.ToByte(Convert.ToUInt16(NoRMG255.Value.ToString()) >> 8);
                            Send_buf[69] = Convert.ToByte((Convert.ToUInt16(NoRMG255.Value.ToString())) & 0xff);
                        }
                        else if (RM67198_rbt_19GP.Checked == true)//RM67198 19绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMG0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMG0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMG2.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMG2.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMG4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMG4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMG8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMG8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMG12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMG12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMG16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMG16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMG20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMG20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMG36.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMG36.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMG52.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMG52.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMG68.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMG68.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMG100.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMG100.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMG132.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMG132.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMG164.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMG164.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMG196.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMG196.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMG228.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMG228.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMG244.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMG244.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMG252.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMG252.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMG254.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMG254.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMG255.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMG255.Value.ToString())) & 0xff);
                        }
                        else//RM67195&RM67295&RM67198 31绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMG0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMG0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMG3.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMG3.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMG4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMG4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMG8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMG8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMG12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMG12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMG16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMG16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMG20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMG20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMG28.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMG28.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMG36.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMG36.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMG44.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMG44.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMG52.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMG52.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMG68.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMG68.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMG84.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMG84.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMG100.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMG100.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMG116.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMG116.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMG132.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMG132.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMG148.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMG148.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMG164.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMG164.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMG180.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMG180.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMG188.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMG188.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMG196.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMG196.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMG204.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMG204.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMG212.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMG212.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMG220.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMG220.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMG228.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMG228.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMG236.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMG236.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMG244.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMG244.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMG248.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMG248.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMG252.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMG252.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMG254.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMG254.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMG255.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMG255.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                case "RM67195_GammaW_Blue":
                    {
                        Send_buf[1] = 0x08;
                        Send_buf[3] |= 0x81;
                        if (RM67198_rbt_33GP.Checked == true)//RM67198 33绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMB0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMB0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMB1.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMB1.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMB2.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMB2.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMB3.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMB3.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMB4.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMB4.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMB8.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMB8.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMB12.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMB12.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMB16.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMB16.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMB20.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMB20.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMB28.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMB28.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMB36.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMB36.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMB44.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMB44.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMB52.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMB52.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMB68.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMB68.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMB84.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMB84.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMB100.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMB100.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMB116.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMB116.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMB132.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMB132.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMB148.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMB148.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMB164.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMB164.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMB180.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMB180.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMB188.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMB188.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMB196.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMB196.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMB204.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMB204.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMB212.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMB212.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMB220.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMB220.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMB228.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMB228.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMB236.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMB236.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMB244.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMB244.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMB248.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMB248.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMB252.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMB252.Value.ToString())) & 0xff);
                            Send_buf[66] = Convert.ToByte(Convert.ToUInt16(NoRMB254.Value.ToString()) >> 8);
                            Send_buf[67] = Convert.ToByte((Convert.ToUInt16(NoRMB254.Value.ToString())) & 0xff);
                            Send_buf[68] = Convert.ToByte(Convert.ToUInt16(NoRMB255.Value.ToString()) >> 8);
                            Send_buf[69] = Convert.ToByte((Convert.ToUInt16(NoRMB255.Value.ToString())) & 0xff);
                        }
                        else if (RM67198_rbt_19GP.Checked == true)//RM67198 19绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMB0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMB0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMB2.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMB2.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMB4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMB4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMB8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMB8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMB12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMB12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMB16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMB16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMB20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMB20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMB36.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMB36.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMB52.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMB52.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMB68.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMB68.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMB100.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMB100.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMB132.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMB132.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMB164.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMB164.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMB196.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMB196.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMB228.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMB228.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMB244.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMB244.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMB252.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMB252.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMB254.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMB254.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMB255.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMB255.Value.ToString())) & 0xff);
                        }
                        else//RM67195&RM67295&RM67198 31绑点
                        {
                            Send_buf[4] = Convert.ToByte(Convert.ToUInt16(NoRMB0.Value.ToString()) >> 8);
                            Send_buf[5] = Convert.ToByte((Convert.ToUInt16(NoRMB0.Value.ToString())) & 0xff);
                            Send_buf[6] = Convert.ToByte(Convert.ToUInt16(NoRMB3.Value.ToString()) >> 8);
                            Send_buf[7] = Convert.ToByte((Convert.ToUInt16(NoRMB3.Value.ToString())) & 0xff);
                            Send_buf[8] = Convert.ToByte(Convert.ToUInt16(NoRMB4.Value.ToString()) >> 8);
                            Send_buf[9] = Convert.ToByte((Convert.ToUInt16(NoRMB4.Value.ToString())) & 0xff);
                            Send_buf[10] = Convert.ToByte(Convert.ToUInt16(NoRMB8.Value.ToString()) >> 8);
                            Send_buf[11] = Convert.ToByte((Convert.ToUInt16(NoRMB8.Value.ToString())) & 0xff);
                            Send_buf[12] = Convert.ToByte(Convert.ToUInt16(NoRMB12.Value.ToString()) >> 8);
                            Send_buf[13] = Convert.ToByte((Convert.ToUInt16(NoRMB12.Value.ToString())) & 0xff);
                            Send_buf[14] = Convert.ToByte(Convert.ToUInt16(NoRMB16.Value.ToString()) >> 8);
                            Send_buf[15] = Convert.ToByte((Convert.ToUInt16(NoRMB16.Value.ToString())) & 0xff);
                            Send_buf[16] = Convert.ToByte(Convert.ToUInt16(NoRMB20.Value.ToString()) >> 8);
                            Send_buf[17] = Convert.ToByte((Convert.ToUInt16(NoRMB20.Value.ToString())) & 0xff);
                            Send_buf[18] = Convert.ToByte(Convert.ToUInt16(NoRMB28.Value.ToString()) >> 8);
                            Send_buf[19] = Convert.ToByte((Convert.ToUInt16(NoRMB28.Value.ToString())) & 0xff);
                            Send_buf[20] = Convert.ToByte(Convert.ToUInt16(NoRMB36.Value.ToString()) >> 8);
                            Send_buf[21] = Convert.ToByte((Convert.ToUInt16(NoRMB36.Value.ToString())) & 0xff);
                            Send_buf[22] = Convert.ToByte(Convert.ToUInt16(NoRMB44.Value.ToString()) >> 8);
                            Send_buf[23] = Convert.ToByte((Convert.ToUInt16(NoRMB44.Value.ToString())) & 0xff);
                            Send_buf[24] = Convert.ToByte(Convert.ToUInt16(NoRMB52.Value.ToString()) >> 8);
                            Send_buf[25] = Convert.ToByte((Convert.ToUInt16(NoRMB52.Value.ToString())) & 0xff);
                            Send_buf[26] = Convert.ToByte(Convert.ToUInt16(NoRMB68.Value.ToString()) >> 8);
                            Send_buf[27] = Convert.ToByte((Convert.ToUInt16(NoRMB68.Value.ToString())) & 0xff);
                            Send_buf[28] = Convert.ToByte(Convert.ToUInt16(NoRMB84.Value.ToString()) >> 8);
                            Send_buf[29] = Convert.ToByte((Convert.ToUInt16(NoRMB84.Value.ToString())) & 0xff);
                            Send_buf[30] = Convert.ToByte(Convert.ToUInt16(NoRMB100.Value.ToString()) >> 8);
                            Send_buf[31] = Convert.ToByte((Convert.ToUInt16(NoRMB100.Value.ToString())) & 0xff);
                            Send_buf[32] = Convert.ToByte(Convert.ToUInt16(NoRMB116.Value.ToString()) >> 8);
                            Send_buf[33] = Convert.ToByte((Convert.ToUInt16(NoRMB116.Value.ToString())) & 0xff);
                            Send_buf[34] = Convert.ToByte(Convert.ToUInt16(NoRMB132.Value.ToString()) >> 8);
                            Send_buf[35] = Convert.ToByte((Convert.ToUInt16(NoRMB132.Value.ToString())) & 0xff);
                            Send_buf[36] = Convert.ToByte(Convert.ToUInt16(NoRMB148.Value.ToString()) >> 8);
                            Send_buf[37] = Convert.ToByte((Convert.ToUInt16(NoRMB148.Value.ToString())) & 0xff);
                            Send_buf[38] = Convert.ToByte(Convert.ToUInt16(NoRMB164.Value.ToString()) >> 8);
                            Send_buf[39] = Convert.ToByte((Convert.ToUInt16(NoRMB164.Value.ToString())) & 0xff);
                            Send_buf[40] = Convert.ToByte(Convert.ToUInt16(NoRMB180.Value.ToString()) >> 8);
                            Send_buf[41] = Convert.ToByte((Convert.ToUInt16(NoRMB180.Value.ToString())) & 0xff);
                            Send_buf[42] = Convert.ToByte(Convert.ToUInt16(NoRMB188.Value.ToString()) >> 8);
                            Send_buf[43] = Convert.ToByte((Convert.ToUInt16(NoRMB188.Value.ToString())) & 0xff);
                            Send_buf[44] = Convert.ToByte(Convert.ToUInt16(NoRMB196.Value.ToString()) >> 8);
                            Send_buf[45] = Convert.ToByte((Convert.ToUInt16(NoRMB196.Value.ToString())) & 0xff);
                            Send_buf[46] = Convert.ToByte(Convert.ToUInt16(NoRMB204.Value.ToString()) >> 8);
                            Send_buf[47] = Convert.ToByte((Convert.ToUInt16(NoRMB204.Value.ToString())) & 0xff);
                            Send_buf[48] = Convert.ToByte(Convert.ToUInt16(NoRMB212.Value.ToString()) >> 8);
                            Send_buf[49] = Convert.ToByte((Convert.ToUInt16(NoRMB212.Value.ToString())) & 0xff);
                            Send_buf[50] = Convert.ToByte(Convert.ToUInt16(NoRMB220.Value.ToString()) >> 8);
                            Send_buf[51] = Convert.ToByte((Convert.ToUInt16(NoRMB220.Value.ToString())) & 0xff);
                            Send_buf[52] = Convert.ToByte(Convert.ToUInt16(NoRMB228.Value.ToString()) >> 8);
                            Send_buf[53] = Convert.ToByte((Convert.ToUInt16(NoRMB228.Value.ToString())) & 0xff);
                            Send_buf[54] = Convert.ToByte(Convert.ToUInt16(NoRMB236.Value.ToString()) >> 8);
                            Send_buf[55] = Convert.ToByte((Convert.ToUInt16(NoRMB236.Value.ToString())) & 0xff);
                            Send_buf[56] = Convert.ToByte(Convert.ToUInt16(NoRMB244.Value.ToString()) >> 8);
                            Send_buf[57] = Convert.ToByte((Convert.ToUInt16(NoRMB244.Value.ToString())) & 0xff);
                            Send_buf[58] = Convert.ToByte(Convert.ToUInt16(NoRMB248.Value.ToString()) >> 8);
                            Send_buf[59] = Convert.ToByte((Convert.ToUInt16(NoRMB248.Value.ToString())) & 0xff);
                            Send_buf[60] = Convert.ToByte(Convert.ToUInt16(NoRMB252.Value.ToString()) >> 8);
                            Send_buf[61] = Convert.ToByte((Convert.ToUInt16(NoRMB252.Value.ToString())) & 0xff);
                            Send_buf[62] = Convert.ToByte(Convert.ToUInt16(NoRMB254.Value.ToString()) >> 8);
                            Send_buf[63] = Convert.ToByte((Convert.ToUInt16(NoRMB254.Value.ToString())) & 0xff);
                            Send_buf[64] = Convert.ToByte(Convert.ToUInt16(NoRMB255.Value.ToString()) >> 8);
                            Send_buf[65] = Convert.ToByte((Convert.ToUInt16(NoRMB255.Value.ToString())) & 0xff);
                        }
                    }
                    break;
                default:
                    break;
            }
            if (RM67198_rbt_33GP.Checked == true)//RM67198 33绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[70], out Send_buf[71]);
            }
            else if (RM67198_rbt_19GP.Checked == true)//RM67198 19绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[42], out Send_buf[43]);
            }
            else  //RM67195&RM67295&RM67198 31绑点
            {
                CRCData.CalculateCrc16(Send_buf, out Send_buf[66], out Send_buf[67]);
            }
            try
            {
                RM19X_0xFE_XX = 0;
                if (RM67198_rbt_33GP.Checked == true)
                {
                    MainForm.comPort.Write(Send_buf, 0, 72);   //RM67198 33绑点
                }
                else if (RM67198_rbt_19GP.Checked == true)
                {
                    MainForm.comPort.Write(Send_buf, 0, 44);   //RM67198 19绑点
                }
                else
                {
                    MainForm.comPort.Write(Send_buf, 0, 68);  //RM67195&RM67295&RM67198 31绑点
                }
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            MainForm.T1.Enabled = false;
            MainForm.T1.Interval = 2000;
            MainForm.T1.Enabled = true;
        }

        private void RM298Reg51Write(int value)
        {
            Send_buf = new byte[8];

            Send_buf[0] = 0x2E;//rm69298
            Send_buf[1] = 0x51;
            Send_buf[2] = 0x03;

            Send_buf[3] = 0x51;
            Send_buf[4] = (byte)((value >> 8) & 0x03);
            Send_buf[5] = (byte)(value & 0xff);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[6], out Send_buf[7]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 8);
            }
            catch
            {
                //MessageBox.Show("串口有误，请检查", "错误");
                textBox1.Text += "串口有误，请检查\r\n";
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }
        private void RM196_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            if (MainForm.IC_comboBox.SelectedIndex == 4)  //if RM67295
            {
                Send_buf[0] = 0x19;
            }
            else if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
            {
                Send_buf[0] = 0x29;
            }
            else if (MainForm.IC_comboBox.SelectedIndex == 10)
            {
                Send_buf[0] = 0x2E;//rm69298
            }
            else
            {
                Send_buf[0] = 0x09; //RM67195
            }
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM67195_RegR":
                    {
                        MessageBox.Show("该功能已经删除！");
                    }
                    break;
                case "RM67195_RegW":         //w写寄存器
                    {
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67195Add.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195Data.Text.Trim(), 16)).ToString());
                        if ((Send_buf[3] == 0xfe) && (Send_buf[4] != 0))
                            RM19X_0xFE_XX = 0;
                    }
                    break;
                case "RM67195_W_Reg51":         //w写寄存器
                    {
                        Send_buf[1] = 0x51;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67195Add.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195Data.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM67195_1_OTPPage": //otp page1
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0A;
                        {       //保存待otp的gamma
                            FileStream fs = new FileStream("../OTP_gamma_195.txt", FileMode.OpenOrCreate, FileAccess.Write);
                            fs.Close();
                            StreamWriter sw = new StreamWriter(fs.Name, false, Encoding.UTF8);
                            string str = null;
                            str = str + NoRMR0.Text + "," + NoRMG0.Text + "," + NoRMB0.Text + "\r\n";
                            str = str + NoRMR3.Text + "," + NoRMG3.Text + "," + NoRMB3.Text + "\r\n";
                            str = str + NoRMR4.Text + "," + NoRMG4.Text + "," + NoRMB4.Text + "\r\n";
                            str = str + NoRMR8.Text + "," + NoRMG8.Text + "," + NoRMB8.Text + "\r\n";
                            str = str + NoRMR12.Text + "," + NoRMG12.Text + "," + NoRMB12.Text + "\r\n";
                            str = str + NoRMR16.Text + "," + NoRMG16.Text + "," + NoRMB16.Text + "\r\n";
                            str = str + NoRMR20.Text + "," + NoRMG20.Text + "," + NoRMB20.Text + "\r\n";
                            str = str + NoRMR28.Text + "," + NoRMG28.Text + "," + NoRMB28.Text + "\r\n";
                            str = str + NoRMR36.Text + "," + NoRMG36.Text + "," + NoRMB36.Text + "\r\n";
                            str = str + NoRMR44.Text + "," + NoRMG44.Text + "," + NoRMB44.Text + "\r\n";
                            str = str + NoRMR52.Text + "," + NoRMG52.Text + "," + NoRMB52.Text + "\r\n";
                            str = str + NoRMR68.Text + "," + NoRMG68.Text + "," + NoRMB68.Text + "\r\n";
                            str = str + NoRMR84.Text + "," + NoRMG84.Text + "," + NoRMB84.Text + "\r\n";
                            str = str + NoRMR100.Text + "," + NoRMG100.Text + "," + NoRMB100.Text + "\r\n";
                            str = str + NoRMR116.Text + "," + NoRMG116.Text + "," + NoRMB116.Text + "\r\n";
                            str = str + NoRMR132.Text + "," + NoRMG132.Text + "," + NoRMB132.Text + "\r\n";
                            str = str + NoRMR148.Text + "," + NoRMG148.Text + "," + NoRMB148.Text + "\r\n";
                            str = str + NoRMR164.Text + "," + NoRMG164.Text + "," + NoRMB164.Text + "\r\n";
                            str = str + NoRMR180.Text + "," + NoRMG180.Text + "," + NoRMB180.Text + "\r\n";
                            str = str + NoRMR188.Text + "," + NoRMG188.Text + "," + NoRMB188.Text + "\r\n";
                            str = str + NoRMR196.Text + "," + NoRMG196.Text + "," + NoRMB196.Text + "\r\n";
                            str = str + NoRMR204.Text + "," + NoRMG204.Text + "," + NoRMB204.Text + "\r\n";
                            str = str + NoRMR212.Text + "," + NoRMG212.Text + "," + NoRMB212.Text + "\r\n";
                            str = str + NoRMR220.Text + "," + NoRMG220.Text + "," + NoRMB220.Text + "\r\n";
                            str = str + NoRMR228.Text + "," + NoRMG228.Text + "," + NoRMB228.Text + "\r\n";
                            str = str + NoRMR236.Text + "," + NoRMG236.Text + "," + NoRMB236.Text + "\r\n";
                            str = str + NoRMR244.Text + "," + NoRMG244.Text + "," + NoRMB244.Text + "\r\n";
                            str = str + NoRMR248.Text + "," + NoRMG248.Text + "," + NoRMB248.Text + "\r\n";
                            str = str + NoRMR252.Text + "," + NoRMG252.Text + "," + NoRMB252.Text + "\r\n";
                            str = str + NoRMR254.Text + "," + NoRMG254.Text + "," + NoRMB254.Text + "\r\n";
                            str = str + NoRMR255.Text + "," + NoRMG255.Text + "," + NoRMB255.Text + "\r\n";
                            sw.WriteLine(str);
                            sw.Dispose(); //释放资源
                            sw.Close(); //关闭数据流
                        }
                    }
                    break;
                case "RM67195_2_OTPPage"://otp page0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0D;
                    }
                    break;
                case "RM67195_3_OTPPage"://otp page2
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0E;
                    }
                    break;
                case "RM67195_4_OTPPage"://otp page3//zangqiang 20171120
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0f;
                    }
                    break;


                //zangqiang 20171120
                case "RM67195_CMD3_ANA_OTPPage"://CMD3_ANA_OTPPage
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x17;
                    }
                    break;
                //zangqiang 20171120

                //zangqiang 20171121
                case "RM67195_OTP_Page_D0"://OTP_Page_D0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x18;
                    }
                    break;
                case "RM67195_OTP_Page_E0"://OTP_Page_E0
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x19;
                    }
                    break;
                //zangqiang 20171121

                //zangqiang 20171125
                case "RM67195_OTP_Page_10"://OTP_Page_10
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x1A;
                    }
                    break;
                case "RM67195_OTP_Page_12"://OTP_Page_12
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x1B;
                    }
                    break;
                //zangqiang 20171125



                case "RM67195_OTPPage_SPR"://otp page5//zangqiang 20171120
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x07;
                    }
                    break;
                case "RM67195_1_OTPtimeR":
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0B;
                        MessageBox.Show("该功能已经删除！");
                    }
                    break;
                case "RM67195_1_OTPtimeW":         //w写寄存器
                    {
                        RM19X_0xFE_XX = 0;
                        Send_buf[1] = 0x0C;
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67195OTP.Text.Trim(), 16)).ToString());
                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            //T1.Enabled = false;
            //T1.Interval = 2000;
            //T1.Enabled = true;
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            text195_255.Text = (Math.Pow((Parttern_RM67195[0] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_254.Text = (Math.Pow((Parttern_RM67195[1] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_252.Text = (Math.Pow((Parttern_RM67195[2] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_248.Text = (Math.Pow((Parttern_RM67195[3] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_244.Text = (Math.Pow((Parttern_RM67195[4] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_236.Text = (Math.Pow((Parttern_RM67195[5] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_228.Text = (Math.Pow((Parttern_RM67195[6] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_220.Text = (Math.Pow((Parttern_RM67195[7] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_212.Text = (Math.Pow((Parttern_RM67195[8] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_204.Text = (Math.Pow((Parttern_RM67195[9] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_196.Text = (Math.Pow((Parttern_RM67195[10] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_188.Text = (Math.Pow((Parttern_RM67195[11] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_180.Text = (Math.Pow((Parttern_RM67195[12] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_164.Text = (Math.Pow((Parttern_RM67195[13] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_148.Text = (Math.Pow((Parttern_RM67195[14] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_132.Text = (Math.Pow((Parttern_RM67195[15] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_116.Text = (Math.Pow((Parttern_RM67195[16] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_100.Text = (Math.Pow((Parttern_RM67195[17] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_84.Text = (Math.Pow((Parttern_RM67195[18] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_68.Text = (Math.Pow((Parttern_RM67195[19] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_52.Text = (Math.Pow((Parttern_RM67195[20] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_44.Text = (Math.Pow((Parttern_RM67195[21] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_36.Text = (Math.Pow((Parttern_RM67195[22] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_28.Text = (Math.Pow((Parttern_RM67195[23] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_20.Text = (Math.Pow((Parttern_RM67195[24] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_16.Text = (Math.Pow((Parttern_RM67195[25] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_12.Text = (Math.Pow((Parttern_RM67195[26] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_8.Text = (Math.Pow((Parttern_RM67195[27] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_4.Text = (Math.Pow((Parttern_RM67195[28] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
            text195_3.Text = (Math.Pow((Parttern_RM67195[29] / 255.0), Convert.ToDouble(textgamma.Text)) * Convert.ToDouble(textlum.Text)).ToString("F4");
        }
        private int adjust_num = 0;
        private string adjust_class = null;
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        //private double lum_off, x_off, y_off;
        private int index = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[31];
        private ushort[] gamma_g = new ushort[31];
        private ushort[] gamma_b = new ushort[31];
        private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;

        private void btnBack_Click(object sender, EventArgs e)
        {
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }


        private void KeyL(object sender, KeyPressEventArgs e)
        {
            Thread.Sleep(100);
            Demura_Click(null, e);
        }

        private double B_x = 0.14;
        private double B_y = 0.06;
        private static bool Gamma_adjust_timeout_flag = false;
        private static int Gamma_auto_adjust_count = 0;
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private void R6645x_Gamma_auto_Adujst(int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable, int steph, int stepm, int stepl,
        double measure_lum, double measure_x, double measure_y, double target_lum, double target_x, double target_y, int adjust_times,
        decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
        //低阶绑点调用
        int gary_last_index2, int gary_last_index1, int gary_target,
        decimal index2_reg_red, decimal index2_reg_green, decimal index2_reg_blue,
        decimal index1_reg_red, decimal index1_reg_green, decimal index1_reg_blue,

        out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)
        {
            int step_tmp = 0;

            timeout_flag = false;
            output_reg_red = input_reg_red;
            output_reg_green = input_reg_green;
            output_reg_blue = input_reg_blue;

            try
            {
                if (Gamma_auto_adjust_count <= adjust_times)
                {
                    Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
                    if (x_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_x - target_x) > target_x * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_x - target_x) > target_x * 5 / 100)
                            {
                                if (target_lum < 1) //if lower target lum
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_x > target_x)
                                output_reg_red = input_reg_red - step_tmp;
                            else output_reg_red = input_reg_red + step_tmp;
                        }
                    }
                    //-----------------------------------------------
                    if (lum_adjust_enable == 1)
                    {
                        if (Math.Abs(measure_lum - target_lum) > target_lum * 20 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepm; }
                            else
                            { step_tmp = steph; }
                        }
                        else if (Math.Abs(measure_lum - target_lum) > target_lum * 2 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepl; }
                            else
                            { step_tmp = stepm; }
                        }
                        else
                        {
                            step_tmp = stepl;
                        }
                        if (measure_lum > target_lum)
                            output_reg_green = input_reg_green - step_tmp;
                        else output_reg_green = input_reg_green + step_tmp;
                    }
                    //-----------------------------------------
                    if (y_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_y - target_y) > target_y * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_y - target_y) > target_y * 5 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_y > target_y)
                                output_reg_blue = input_reg_blue - step_tmp;
                            else output_reg_blue = input_reg_blue + step_tmp;
                        }
                    }
                    //----------------------------------若数值超限--------------------------------------

                    if (((output_reg_red < 30) || (output_reg_red > index2_reg_red) || (output_reg_green < 30) || (output_reg_green > index2_reg_green) || (output_reg_blue < 30) || (output_reg_blue > index2_reg_blue)) && ((target_lum * 95 / 100 < measure_lum) && (target_lum * 105 / 100 > measure_lum)))
                    {
                        output_reg_red = Convert.ToInt16(index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_green = Convert.ToInt16(index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_blue = Convert.ToInt16(index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        textBox1.Text = "该绑点R/G/B插值迭代\r\n";
                        Gamma_auto_adjust_count = 0;
                        timeout_flag = true;
                    }
                }
                else
                {
                    textBox1.Text += "Timeout!\r\n";
                    Gamma_auto_adjust_count = 0;
                    timeout_flag = true;

                    if (output_reg_red > index1_reg_red)
                    {
                        output_reg_red = index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "该绑点R差值迭代/r/n";
                    }
                    if (output_reg_green > index1_reg_green)
                    {
                        output_reg_green = index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "该绑点G差值迭代/r/n";
                    }
                    if (output_reg_blue > index1_reg_blue)
                    {
                        output_reg_blue = index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "该绑点B差值迭代/r/n";
                    }
                }
                output_reg_red = Convert.ToInt32(output_reg_red);
                output_reg_green = Convert.ToInt32(output_reg_green);
                output_reg_blue = Convert.ToInt32(output_reg_blue);
            }
            catch
            {
                ;
            }

        }

        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x000f) || (datag < 0x000f) || (Datab < 0x000f) || (datar > 0x0ff0) || (datag > 0x0ff0) || (Datab > 0x0ff0))
            {
                T5.Enabled = false;
                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                //MessageBox.Show("数值超出界限，已停止gamma调试", "提示");
                textBox1.Text += "异常Gamma数据！\r\n"; //("写入数据检查：数值超出界限，已停止gamma调试", "提示");
                //把光标（文本起始点）移动到文件框中，文本的末尾处
                this.textBox1.SelectionStart = this.textBox1.TextLength;
                //然后滚动到光标所在的位置
                this.textBox1.ScrollToCaret();
            }

            switch (MainForm.IC_comboBox.SelectedIndex)
            {
                case 0:
                    Send_buf[0] = 0x09;//195
                    break;
                case 1:
                    Send_buf[0] = 0x08;//120
                    break;
                case 2:
                    Send_buf[0] = 0x0E;//160
                    break;
                case 3:
                    Send_buf[0] = 0x1E;//162
                    break;
                case 4:
                    Send_buf[0] = 0x19;//295
                    break;
                case 5:
                    Send_buf[0] = 0x29;//198
                    break;
                case 8:
                    Send_buf[0] = 0x2C;//300
                    break;
                case 33:
                case 10:
                    Send_buf[0] = 0x2e;//298
                    break;
                case 11:
                    Send_buf[0] = 0x3C;//350
                    break;
                case 12:
                    Send_buf[0] = 0x3D;//010
                    break;
                case 13:
                    Send_buf[0] = 0x30;//R66455
                    break;
                default:
                    break;
            }
            //Send_buf[0] = 0x2E;
            Send_buf[1] = 0xDD;
            Send_buf[2] = 12;
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
                return;
            }
        }

        private void btnAutoGamma_Click(object sender, EventArgs e)//255阶为例
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[30, 3];
            D_lum = new double[30];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            btnGammaManuAdj.Enabled = true;
            btnGammaManuAdjAuto.Enabled = true;
            //------------flexible gamma use------------------
            Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            D_lum[0] = Convert.ToDouble(text195_255.Text);
            D_lum[1] = Convert.ToDouble(text195_254.Text);
            D_lum[2] = Convert.ToDouble(text195_252.Text);
            D_lum[3] = Convert.ToDouble(text195_248.Text);
            D_lum[4] = Convert.ToDouble(text195_244.Text);
            D_lum[5] = Convert.ToDouble(text195_236.Text);
            D_lum[6] = Convert.ToDouble(text195_228.Text);
            D_lum[7] = Convert.ToDouble(text195_220.Text);
            D_lum[8] = Convert.ToDouble(text195_212.Text);
            D_lum[9] = Convert.ToDouble(text195_204.Text);
            D_lum[10] = Convert.ToDouble(text195_196.Text);
            D_lum[11] = Convert.ToDouble(text195_188.Text);
            D_lum[12] = Convert.ToDouble(text195_180.Text);
            D_lum[13] = Convert.ToDouble(text195_164.Text);
            D_lum[14] = Convert.ToDouble(text195_148.Text);
            D_lum[15] = Convert.ToDouble(text195_132.Text);
            D_lum[16] = Convert.ToDouble(text195_116.Text);
            D_lum[17] = Convert.ToDouble(text195_100.Text);
            D_lum[18] = Convert.ToDouble(text195_84.Text);
            D_lum[19] = Convert.ToDouble(text195_68.Text);
            D_lum[20] = Convert.ToDouble(text195_52.Text);
            D_lum[21] = Convert.ToDouble(text195_44.Text);
            D_lum[22] = Convert.ToDouble(text195_36.Text);
            D_lum[23] = Convert.ToDouble(text195_28.Text);
            D_lum[24] = Convert.ToDouble(text195_20.Text);
            D_lum[25] = Convert.ToDouble(text195_16.Text);
            D_lum[26] = Convert.ToDouble(text195_12.Text);
            D_lum[27] = Convert.ToDouble(text195_8.Text);
            D_lum[28] = Convert.ToDouble(text195_4.Text);
            D_lum[29] = Convert.ToDouble(text195_3.Text);

            D_x = Convert.ToDouble(textx.Text);
            D_y = Convert.ToDouble(texty.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 30; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            //295 and 198
            {
                gamma_r = GAMMAs.RM295_Gamma_r;
                gamma_g = GAMMAs.RM295_Gamma_g;
                gamma_b = GAMMAs.RM295_Gamma_b;
            }

            txtRM67195Add.Text = "FE";
            if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)
            {
                if (RM67198_rbt_HBM.Checked == true)
                {
                    txtRM67195Data.Text = "51";
                }
                else
                {
                    txtRM67195Data.Text = "50";
                }
            }
            else if ((MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298) || (MainForm.IC_comboBox.SelectedIndex == 33))
            {
                if (RM67198_rbt_HBM.Checked == true)
                {
                    txtRM67195Data.Text = "52";
                }
                else
                {
                    txtRM67195Data.Text = "50";
                }

            }
            else
                txtRM67195Data.Text = "05";
            RM196_Others_Click(RM67195_RegW, e);


            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(textlum_off.Text);
                        x_off = Convert.ToDouble(textx_off.Text);
                        y_off = Convert.ToDouble(texty_off.Text);
                        OLED_Show_Click(btnwhite, e);
                        T5.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void btnAutogammas_VisibleChanged(object sender, EventArgs e)
        {

        }

        private void rm69298_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                //MainForm.comPort.Close();
                //Thread.Sleep(5);
                MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);

                Thread.Sleep(5);
                MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                Thread.Sleep(5);
                //MainForm.comPort.Open();
                textBox1.Text += "com 已经连接！\r\n";
            }
            else
            {
                //MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);

            }
        }

        private void T5_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr = 0, lumg = 0, lumb = 0;
            Msr_CA310();

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
            {
                flag1 = 0;
            }
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
            {
                flag2 = 0;
            }
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
            {
                flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T5.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                Gamma_auto_adjust_count = 0;
                //--------
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 30)
                {
                    RM67195_GAMMA_Click(RM67195_GammaW_Red, null);
                    RM67195_GAMMA_Click(RM67195_GammaW_Green, null);
                    RM67195_GAMMA_Click(RM67195_GammaW_Blue, null);
                    textBox1.Text = textBox1.Text + "RM67195/295/198 gamma调试完毕!\r\n";
                }
                if (index <= 29)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(text195_254_off.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(text195_252_off.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(text195_248_off.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(text195_244_off.Text);
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(text195_236_off.Text);
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(text195_228_off.Text);
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(text195_220_off.Text);
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(text195_212_off.Text);
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(text195_204_off.Text);
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(text195_196_off.Text);
                            break;
                        case 11:
                            lum_off = Convert.ToDouble(text195_188_off.Text);
                            break;
                        case 12:
                            lum_off = Convert.ToDouble(text195_180_off.Text);
                            break;
                        case 13:
                            lum_off = Convert.ToDouble(text195_164_off.Text);
                            break;
                        case 14:
                            lum_off = Convert.ToDouble(text195_148_off.Text);
                            break;
                        case 15:
                            lum_off = Convert.ToDouble(text195_132_off.Text) * Convert.ToDouble(text195_132.Text) / 100;
                            break;
                        case 16:
                            lum_off = Convert.ToDouble(text195_116_off.Text) * Convert.ToDouble(text195_116.Text) / 100;
                            break;
                        case 17:
                            lum_off = Convert.ToDouble(text195_100_off.Text) * Convert.ToDouble(text195_100.Text) / 100;
                            break;
                        case 18:
                            lum_off = Convert.ToDouble(text195_84_off.Text) * Convert.ToDouble(text195_84.Text) / 100;
                            break;
                        case 19:
                            lum_off = Convert.ToDouble(text195_68_off.Text) * Convert.ToDouble(text195_68.Text) / 100;
                            break;
                        case 20:
                            lum_off = Convert.ToDouble(text195_52_off.Text) * Convert.ToDouble(text195_52.Text) / 100;
                            break;
                        case 21:
                            lum_off = Convert.ToDouble(text195_44_off.Text) * Convert.ToDouble(text195_44.Text) / 100;
                            break;
                        case 22:
                            lum_off = Convert.ToDouble(text195_36_off.Text) * Convert.ToDouble(text195_36.Text) / 100;
                            break;
                        case 23:
                            lum_off = Convert.ToDouble(text195_28_off.Text) * Convert.ToDouble(text195_28.Text) / 100;
                            break;
                        case 24:
                            lum_off = Convert.ToDouble(text195_20_off.Text) * Convert.ToDouble(text195_20.Text) / 100;
                            break;
                        case 25:
                            lum_off = Convert.ToDouble(text195_16_off.Text) * Convert.ToDouble(text195_16.Text) / 100;
                            break;
                        case 26:
                            lum_off = Convert.ToDouble(text195_12_off.Text) * Convert.ToDouble(text195_12.Text) / 100;
                            break;
                        case 27:
                            lum_off = Convert.ToDouble(text195_8_off.Text) * Convert.ToDouble(text195_8.Text) / 100;
                            break;
                        case 28:
                            lum_off = Convert.ToDouble(text195_4_off.Text) * Convert.ToDouble(text195_4.Text) / 100;
                            break;
                        case 29:
                            lum_off = Convert.ToDouble(text195_3_off.Text) * Convert.ToDouble(text195_3.Text) / 100;
                            break;
                    }

                    numPatternR.Value = Parttern_RM67195[index];
                    numPatternG.Value = Parttern_RM67195[index];
                    numPatternB.Value = Parttern_RM67195[index];
                    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(15); //1秒=1000毫秒，
                    T5.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r, g, b;
                int flexible_gamma_count = 100;

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR255.Value, NoRMG255.Value, NoRMB255.Value, 255, 255, 255, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR255.Value = r;
                                NoRMG255.Value = g;
                                NoRMB255.Value = b;
                                auto_gamma_write(gamma_r[30], Convert.ToUInt16(NoRMR255.Value), gamma_g[30], Convert.ToUInt16(NoRMG255.Value), gamma_b[30], Convert.ToUInt16(NoRMB255.Value));
                            }
                            break;
                        case 1:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, 255, 255, 254, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR254.Value = r;
                                NoRMG254.Value = g;
                                NoRMB254.Value = b;
                                auto_gamma_write(gamma_r[29], Convert.ToUInt16(NoRMR254.Value), gamma_g[29], Convert.ToUInt16(NoRMG254.Value), gamma_b[29], Convert.ToUInt16(NoRMB254.Value));

                            }
                            break;
                        case 2:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, 255, 254, 252, NoRMR255.Value, NoRMG255.Value, NoRMB255.Value, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR252.Value = r;
                                NoRMG252.Value = g;
                                NoRMB252.Value = b;
                                auto_gamma_write(gamma_r[28], Convert.ToUInt16(NoRMR252.Value), gamma_g[28], Convert.ToUInt16(NoRMG252.Value), gamma_b[28], Convert.ToUInt16(NoRMB252.Value));

                            }
                            break;
                        case 3:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, 254, 252, 248, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR248.Value = r;
                                NoRMG248.Value = g;
                                NoRMB248.Value = b;
                                auto_gamma_write(gamma_r[27], Convert.ToUInt16(NoRMR248.Value), gamma_g[27], Convert.ToUInt16(NoRMG248.Value), gamma_b[27], Convert.ToUInt16(NoRMB248.Value));

                            }
                            break;
                        case 4:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, 252, 248, 244, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR244.Value = r;
                                NoRMG244.Value = g;
                                NoRMB244.Value = b;
                                auto_gamma_write(gamma_r[26], Convert.ToUInt16(NoRMR244.Value), gamma_g[26], Convert.ToUInt16(NoRMG244.Value), gamma_b[26], Convert.ToUInt16(NoRMB244.Value));

                            }
                            break;
                        case 5:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, 248, 244, 236, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR236.Value = r;
                                NoRMG236.Value = g;
                                NoRMB236.Value = b;
                                auto_gamma_write(gamma_r[25], Convert.ToUInt16(NoRMR236.Value), gamma_g[25], Convert.ToUInt16(NoRMG236.Value), gamma_b[25], Convert.ToUInt16(NoRMB236.Value));


                            }
                            break;
                        case 6:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, 244, 236, 228, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR228.Value = r;
                                NoRMG228.Value = g;
                                NoRMB228.Value = b;
                                auto_gamma_write(gamma_r[24], Convert.ToUInt16(NoRMR228.Value), gamma_g[24], Convert.ToUInt16(NoRMG228.Value), gamma_b[24], Convert.ToUInt16(NoRMB228.Value));

                            }
                            break;
                        case 7:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, 236, 228, 220, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR220.Value = r;
                                NoRMG220.Value = g;
                                NoRMB220.Value = b;
                                auto_gamma_write(gamma_r[23], Convert.ToUInt16(NoRMR220.Value), gamma_g[23], Convert.ToUInt16(NoRMG220.Value), gamma_b[23], Convert.ToUInt16(NoRMB220.Value));

                            }
                            break;
                        case 8:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, 228, 220, 212, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR212.Value = r;
                                NoRMG212.Value = g;
                                NoRMB212.Value = b;
                                auto_gamma_write(gamma_r[22], Convert.ToUInt16(NoRMR212.Value), gamma_g[22], Convert.ToUInt16(NoRMG212.Value), gamma_b[22], Convert.ToUInt16(NoRMB212.Value));

                            }
                            break;
                        case 9:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, 220, 212, 204, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR204.Value = r;
                                NoRMG204.Value = g;
                                NoRMB204.Value = b;
                                auto_gamma_write(gamma_r[21], Convert.ToUInt16(NoRMR204.Value), gamma_g[21], Convert.ToUInt16(NoRMG204.Value), gamma_b[21], Convert.ToUInt16(NoRMB204.Value));

                            }
                            break;
                        case 10:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, 212, 204, 196, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR196.Value = r;
                                NoRMG196.Value = g;
                                NoRMB196.Value = b;
                                auto_gamma_write(gamma_r[20], Convert.ToUInt16(NoRMR196.Value), gamma_g[20], Convert.ToUInt16(NoRMG196.Value), gamma_b[20], Convert.ToUInt16(NoRMB196.Value));

                            }
                            break;
                        case 11:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, 204, 196, 188, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR188.Value = r;
                                NoRMG188.Value = g;
                                NoRMB188.Value = b;
                                auto_gamma_write(gamma_r[19], Convert.ToUInt16(NoRMR188.Value), gamma_g[19], Convert.ToUInt16(NoRMG188.Value), gamma_b[19], Convert.ToUInt16(NoRMB188.Value));

                            }
                            break;
                        case 12:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, 196, 188, 180, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR180.Value = r;
                                NoRMG180.Value = g;
                                NoRMB180.Value = b;
                                auto_gamma_write(gamma_r[18], Convert.ToUInt16(NoRMR180.Value), gamma_g[18], Convert.ToUInt16(NoRMG180.Value), gamma_b[18], Convert.ToUInt16(NoRMB180.Value));

                            }
                            break;
                        case 13:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, 188, 180, 164, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR164.Value = r;
                                NoRMG164.Value = g;
                                NoRMB164.Value = b;
                                auto_gamma_write(gamma_r[17], Convert.ToUInt16(NoRMR164.Value), gamma_g[17], Convert.ToUInt16(NoRMG164.Value), gamma_b[17], Convert.ToUInt16(NoRMB164.Value));

                            }
                            break;
                        case 14:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, 180, 164, 148, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR148.Value = r;
                                NoRMG148.Value = g;
                                NoRMB148.Value = b;
                                auto_gamma_write(gamma_r[16], Convert.ToUInt16(NoRMR148.Value), gamma_g[16], Convert.ToUInt16(NoRMG148.Value), gamma_b[16], Convert.ToUInt16(NoRMB148.Value));

                            }
                            break;
                        case 15:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, 164, 148, 132, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR132.Value = r;
                                NoRMG132.Value = g;
                                NoRMB132.Value = b;
                                auto_gamma_write(gamma_r[15], Convert.ToUInt16(NoRMR132.Value), gamma_g[15], Convert.ToUInt16(NoRMG132.Value), gamma_b[15], Convert.ToUInt16(NoRMB132.Value));

                            }
                            break;
                        case 16:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, 148, 132, 116, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR116.Value = r;
                                NoRMG116.Value = g;
                                NoRMB116.Value = b;
                                auto_gamma_write(gamma_r[14], Convert.ToUInt16(NoRMR116.Value), gamma_g[14], Convert.ToUInt16(NoRMG116.Value), gamma_b[14], Convert.ToUInt16(NoRMB116.Value));

                            }
                            break;
                        case 17:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, 132, 116, 100, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR100.Value = r;
                                NoRMG100.Value = g;
                                NoRMB100.Value = b;
                                auto_gamma_write(gamma_r[13], Convert.ToUInt16(NoRMR100.Value), gamma_g[13], Convert.ToUInt16(NoRMG100.Value), gamma_b[13], Convert.ToUInt16(NoRMB100.Value));

                            }
                            break;
                        case 18:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, 116, 100, 84, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR84.Value = r;
                                NoRMG84.Value = g;
                                NoRMB84.Value = b;
                                auto_gamma_write(gamma_r[12], Convert.ToUInt16(NoRMR84.Value), gamma_g[12], Convert.ToUInt16(NoRMG84.Value), gamma_b[12], Convert.ToUInt16(NoRMB84.Value));

                            }
                            break;
                        case 19:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, 100, 84, 68, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR68.Value = r;
                                NoRMG68.Value = g;
                                NoRMB68.Value = b;
                                auto_gamma_write(gamma_r[11], Convert.ToUInt16(NoRMR68.Value), gamma_g[11], Convert.ToUInt16(NoRMG68.Value), gamma_b[11], Convert.ToUInt16(NoRMB68.Value));

                            }
                            break;
                        case 20:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, 84, 68, 52, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR52.Value = r;
                                NoRMG52.Value = g;
                                NoRMB52.Value = b;
                                auto_gamma_write(gamma_r[10], Convert.ToUInt16(NoRMR52.Value), gamma_g[10], Convert.ToUInt16(NoRMG52.Value), gamma_b[10], Convert.ToUInt16(NoRMB52.Value));

                            }
                            break;
                        case 21:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, 68, 52, 44, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR44.Value = r;
                                NoRMG44.Value = g;
                                NoRMB44.Value = b;
                                auto_gamma_write(gamma_r[9], Convert.ToUInt16(NoRMR44.Value), gamma_g[9], Convert.ToUInt16(NoRMG44.Value), gamma_b[9], Convert.ToUInt16(NoRMB44.Value));

                            }
                            break;
                        case 22:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, 52, 44, 36, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR36.Value = r;
                                NoRMG36.Value = g;
                                NoRMB36.Value = b;
                                auto_gamma_write(gamma_r[8], Convert.ToUInt16(NoRMR36.Value), gamma_g[8], Convert.ToUInt16(NoRMG36.Value), gamma_b[8], Convert.ToUInt16(NoRMB36.Value));

                            }
                            break;
                        case 23:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, 44, 36, 28, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR28.Value = r;
                                NoRMG28.Value = g;
                                NoRMB28.Value = b;
                                auto_gamma_write(gamma_r[7], Convert.ToUInt16(NoRMR28.Value), gamma_g[7], Convert.ToUInt16(NoRMG28.Value), gamma_b[7], Convert.ToUInt16(NoRMB28.Value));

                            }
                            break;
                        case 24://20阶以下
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, 36, 28, 20, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR20.Value = r;
                                NoRMG20.Value = g;
                                NoRMB20.Value = b;
                                auto_gamma_write(gamma_r[6], Convert.ToUInt16(NoRMR20.Value), gamma_g[6], Convert.ToUInt16(NoRMG20.Value), gamma_b[6], Convert.ToUInt16(NoRMB20.Value));

                            }
                            break;
                        case 25:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, 28, 20, 16, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR16.Value = r;
                                NoRMG16.Value = g;
                                NoRMB16.Value = b;
                                auto_gamma_write(gamma_r[5], Convert.ToUInt16(NoRMR16.Value), gamma_g[5], Convert.ToUInt16(NoRMG16.Value), gamma_b[5], Convert.ToUInt16(NoRMB16.Value));

                            }
                            break;
                        case 26:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, 20, 16, 12, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR12.Value = r;
                                NoRMG12.Value = g;
                                NoRMB12.Value = b;
                                auto_gamma_write(gamma_r[4], Convert.ToUInt16(NoRMR12.Value), gamma_g[4], Convert.ToUInt16(NoRMG12.Value), gamma_b[4], Convert.ToUInt16(NoRMB12.Value));

                            }
                            break;
                        case 27:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, 16, 12, 8, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR8.Value = r;
                                NoRMG8.Value = g;
                                NoRMB8.Value = b;
                                auto_gamma_write(gamma_r[3], Convert.ToUInt16(NoRMR8.Value), gamma_g[3], Convert.ToUInt16(NoRMG8.Value), gamma_b[3], Convert.ToUInt16(NoRMB8.Value));

                            }
                            break;
                        case 28:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR4.Value, NoRMG4.Value, NoRMB4.Value, 12, 8, 4, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR4.Value = r;
                                NoRMG4.Value = g;
                                NoRMB4.Value = b;
                                auto_gamma_write(gamma_r[2], Convert.ToUInt16(NoRMR4.Value), gamma_g[2], Convert.ToUInt16(NoRMG4.Value), gamma_b[2], Convert.ToUInt16(NoRMB4.Value));

                            }
                            break;
                        case 29:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], 2, NoRMR3.Value, NoRMG3.Value, NoRMB3.Value, 8, 4, 3, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, NoRMR4.Value, NoRMG4.Value, NoRMB4.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                NoRMR3.Value = r;
                                NoRMG3.Value = g;
                                NoRMB3.Value = b;
                                auto_gamma_write(gamma_r[1], Convert.ToUInt16(NoRMR3.Value), gamma_g[1], Convert.ToUInt16(NoRMG3.Value), GAMMAs.RM195_Gamma_b[1], Convert.ToUInt16(NoRMB3.Value));

                            }
                            break;
                    }
                }
                catch
                {
                    T5.Enabled = false;
                    MessageBox.Show("数值超限！");
                }
            }
        }
        private void btn195Stop_Click(object sender, EventArgs e)
        {
            T5.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            //--------reset---------------
            Gamma_adjust_timeout_flag = false;
            Gamma_auto_adjust_count = 0;
            //----------------------------
        }
        private void RM692Ax_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }
        #region
        private void BtnGammaManuAdjStop_Click(object sender, EventArgs e)
        {
            TimGammaManuAdj.Enabled = false;
            TimGammaManuAdj.Tag = "stop";
        }

        private void TimGammaManuAdj_Tick(object sender, EventArgs e)
        {
            TimGammaManuAdj.Enabled = false;
            BtnGammaManuAdj_Click(null, e);
            if (Convert.ToString(TimGammaManuAdj.Tag) != "stop")
            {
                TimGammaManuAdj.Enabled = true;
            }
        }

        private void BtnGammaManuAdjAuto_Click(object sender, EventArgs e)
        {
            TimGammaManuAdj.Enabled = true;
            TimGammaManuAdj.Tag = "";
            textBox1.Clear(); str = "Point,R,G,B,Lum,x,y\r\n";
        }

        private void BtntxtClean_Click(object sender, EventArgs e)
        {
            textBox1.Clear(); str = "Point,R,G,B,Lum,x,y\r\n";
        }

        private void BtnGammaManuAdjDataSave_Click(object sender, EventArgs e)
        {
            //保存
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型


            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);

                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("DW数据保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("DW数据保存时发生错误，请再试！", "系统提示：");
                }
            }
        }
        string str = "Point,R,G,B,Lum,x,y\r\n";
        private void BtnGammaManuAdj_Click(object sender, EventArgs e)
        {
            gamma_r = GAMMAs.RM295_Gamma_r;
            gamma_g = GAMMAs.RM295_Gamma_g;
            gamma_b = GAMMAs.RM295_Gamma_b;
            //TimGammaManuAdj.Enabled = false;
            if ((numGammaR.Value - numGammaAdjStep.Value >= 0) && (chkSelGammaR.Checked))
            {
                numGammaR.Value -= numGammaAdjStep.Value;
            }
            if ((numGammaG.Value - numGammaAdjStep.Value >= 0) && (chkSelGammaG.Checked))
            {
                numGammaG.Value -= numGammaAdjStep.Value;
            }
            if ((numGammaB.Value - numGammaAdjStep.Value >= 0) && (chkSelGammaB.Checked))
            {
                numGammaB.Value -= numGammaAdjStep.Value;
            }
            auto_gamma_write(gamma_r[Convert.ToUInt16(numGammaPointN.Value)], Convert.ToUInt16(numGammaR.Value), gamma_g[Convert.ToUInt16(numGammaPointN.Value)], Convert.ToUInt16(numGammaG.Value), gamma_b[Convert.ToUInt16(numGammaPointN.Value)], Convert.ToUInt16(numGammaB.Value));
            Thread.Sleep(500);
            btn_CAS200_Synt_Click(btn_CAS200_Measure, null);
            str += numGammaPointN.Value + "," + numGammaR.Value + "," + numGammaG.Value + "," + numGammaB.Value + "," + auto_lum.ToString("F3") + "," + auto_x.ToString("F3") + "," + auto_y.ToString("F3") + "\r\n";
            textBox1.Text += str;
            //TimGammaManuAdj.Enabled = false;
        }
        #endregion
    }
}
